2 ms·
Given the JSON string: {"cities": [{"name": "London"}, {"name": "New York"}]} Parsed into a variable called 'root' with either 'new JSONObject(...)' or 'j
by twic 12y ago
Given the JSON string:
{"cities": [{"name": "London"}, {"name": "New York"}]}
Parsed into a variable called 'root' with either 'new JSONObject(...)' or 'json.loads(...)', compare:
root.getJSONArray("cities").getJSONObject(0).getString("name");
To:
root['cities'][0]['name']
The Python version is the sort of thing you find yourself writing in Python anyway (at least, if you write tabloid-level Python the way i do); it's reasonably idiomatic. The Java version is cumbersome and feels very un-Javaish.
Bear in mind that with SOAP, in Java, by that point you would have mangled the data into realistic-looking objects, and would write something like:
root.getCities().get(0).getName();