How to do it ?
-
If you meet
{}in your code , you can useJSONObjectto parse it . -
If you meet
[]in your code , you can useJSONArrayto parse it . -
And if you meet
[]in your code , you can usefor loopto get value in it . -
And you should use
try catchin your code .
Try this in your code .
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray elements = jsonObject.getJSONArray("elements");
for (int i = 0; i < elements.length(); i++) {
JSONObject jsonObject1 = elements.getJSONObject(i);
String name = jsonObject1.optString("name");
String value = jsonObject1.optString("value");
JSONObject format = jsonObject1.optJSONObject("format");
JSONObject Text = format.optJSONObject("Text");
String orientation = Text.optString("orientation");
String height = Text.optString("height");
String width = Text.optString("width");
}
} catch (JSONException e) {
e.printStackTrace();
}
Note
-
And use
optStringandoptJSONObjectin your code . Ifformatisnull,it will not return error . -
And you can judge that
JSONArray‘slengthis not 0 and notnullin the code .
1
solved how to get elements from JSONObject and write it to file