The org.json package in Java provides classes for handling JSON data. The JSONObject class in this package provides a method called "optDouble" which returns the value associated with a given key as a double.
Example 1:
JSONObject jsonObject = new JSONObject("{\"key\": 5.5}"); double value = jsonObject.optDouble("key"); System.out.println(value); // Output: 5.5
In this example, we created a JSONObject with a key-value pair where the value is a double. We then called optDouble method to retrieve the value associated with the key and stored it in a double variable. Finally, we printed the value.
Example 2:
JSONObject jsonObject = new JSONObject("{\"key\": \"value\"}"); double value = jsonObject.optDouble("key"); System.out.println(value); // Output: NaN
In this example, we created a JSONObject with a key-value pair where the value is a string. When we tried to retrieve the value using optDouble, it returned NaN (Not a Number) since the value cannot be cast to a double.
The org.json package is the library used in these examples.
Java JSONObject.optDouble - 30 examples found. These are the top rated real world Java examples of org.json.JSONObject.optDouble extracted from open source projects. You can rate examples to help us improve the quality of examples.