JSONObject.getString() is a method in the org.json library that extracts a string value from a JSON object. This method is useful when you want to get the value of a specific key from a JSON object as a string.
Example 1:
Suppose you have a JSON object that looks like this:
{ "name": "John", "age": 30, "gender": "male" }
To get the value of the "name" key, you can use the getString() method as follows:
JSONObject obj = new JSONObject(jsonString); String name = obj.getString("name");
In this example, jsonString is a string representation of the above JSON object.
Example 2:
Suppose you have a JSON object that looks like this:
To get the value of the "name" key inside the "person" object, you can use the getString() method as follows:
JSONObject obj = new JSONObject(jsonString); String name = obj.getJSONObject("person").getString("name");
In this example, jsonString is a string representation of the above JSON object. We first extract the "person" object using the getJSONObject() method, and then extract the "name" value from it using the getString() method.
Note: The org.json library is not included in the Java SDK by default, so you need to download it separately from the internet.
Java JSONObject.getString - 30 examples found. These are the top rated real world Java examples of org.json.JSONObject.getString extracted from open source projects. You can rate examples to help us improve the quality of examples.