| [ Web Proxy ] |
| Viewing: https://processing.org/reference/parseJSONObject_.html | [Back] [Original] |
Takes a String, parses its contents, and returns a
JSONObject. If the String does not contain JSONObject
data or cannot be parsed, a null value is returned.
parseJSONObject() is most useful when pulling data dynamically, such
as from third-party APIs. Normally, API results would be saved to a
String, and then can be converted to a structured JSONObject
using parseJSONObject(). Be sure to check if null is returned
before performing operations on the new JSONObject in case the
String content could not be parsed.
If your data already exists as a JSON file in the data folder, it is
simpler to useloadJSONObject().
String data = "{ \"id\": 0, \"species\": \"Panthera leo\", \"name\": \"Lion\"}";
void setup() {
JSONObject json = parseJSONObject(data);
if (json == null) {
println("JSONObject could not be parsed");
} else {
String species = json.getString("species");
println(species);
}
}
// Sketch prints:
// Panthera leo
parseJSONObject(input)input(String)String to parse as aJSONObjectJSONObject
[Creative Commons License]This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
| Web Proxy Viewer | New URL | Original Page |