How to do it ?
-
If you meet
{}
in your code , you can useJSONObject
to parse it . -
If you meet
[]
in your code , you can useJSONArray
to parse it . -
And if you meet
[]
in your code , you can usefor loop
to get value in it . -
And you should use
try catch
in 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
optString
andoptJSONObject
in your code . Ifformat
isnull
,it will not return error . -
And you can judge that
JSONArray
‘slength
is not 0 and notnull
in the code .
1
solved how to get elements from JSONObject and write it to file