[Solved] how to get elements from JSONObject and write it to file


How to do it ?

  • If you meet {} in your code , you can use JSONObject to parse it .

  • If you meet [] in your code , you can use JSONArray to parse it .

  • And if you meet [] in your code , you can use for 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 and optJSONObject in your code . If format is null ,it will not return error .

  • And you can judge that JSONArray‘s length is not 0 and not null in the code .

1

solved how to get elements from JSONObject and write it to file