0

i want to know select value in JSON array.

my JSON is

{
  "meta": {
    "is_end": true,
    "total_count": 1,
    "pageable_count": 1
  },
  "documents": [
    {
      "road_address": {
        "undergroun_yn": "N",
        "road_name": "충대로",
        "underground_yn": "N",
        "region_2depth_name": "청주시 서원구",
        "zone_no": "28644",
        "sub_building_no": "",
        "region_3depth_name": "개신동",
        "main_building_no": "1",
        "address_name": "충북 청주시 서원구 충대로 1",
        "y": "36.63003935314667",
        "x": "127.45479196889647",
        "region_1depth_name": "충북",
        "building_name": "충북대학교"
      },
      "address_name": "충북 청주시 서원구 충대로 1",
      "address": {
        "b_code": "4311210900",
        "region_3depth_h_name": "성화.개신.죽림동",
        "main_address_no": "12",
        "h_code": "4311259000",
        "region_2depth_name": "청주시 서원구",
        "main_adderss_no": "12",
        "sub_address_no": "",
        "region_3depth_name": "개신동",
        "address_name": "충북 청주시 서원구 개신동 12",
        "y": "36.6284039605616",
        "x": "127.45921419389084",
        "mountain_yn": "N",
        "zip_code": "361763",
        "region_1depth_name": "충북",
        "sub_adderss_no": ""
      },
      "y": "36.63003935314667",
      "x": "127.45479196889647",
      "address_type": "ROAD_ADDR"
    }
  ]
}

but, this code error is occurred that say "java.lang.IllegalStateException: Not a JSON Object" so, i try to solve them and search many way. i don't using a part of 'meta' only using 'documents' in JSON. than, i write the java code. my original code is

JsonElement jsonElement = parser.parse(html.toString()).getAsJsonObject();
JsonObject rootObj = jsonElement.getAsJsonObject();


        JsonObject element = rootObj.get("documents").getAsJsonObject();
        String y = element.getAsJsonObject().get("x").toString();
        String x = element.getAsJsonObject().get("y").toString();

        System.out.println(y);
        System.out.println(x);
        System.out.println(html.toString());
        return html.toString();

and i knew "documents" is JSON array so, i revise my code. my revised java code is

    JsonElement jsonElement = parser.parse(html.toString()).getAsJsonObject();
    JsonObject rootObj = jsonElement.getAsJsonObject();
    String y = rootObj.getAsJsonArray().get(Integer.parseInt("x")).toString();
    String x = rootObj.getAsJsonArray().get(Integer.parseInt("y")).toString();

        System.out.println(y);
        System.out.println(x);
        System.out.println(html.toString());
        return html.toString();

but also error is occurred that say "java.lang.IllegalStateException: Not a JSON Array" i want to get result like

{
"documents": [
        {
          "road_address": {
            "undergroun_yn": "N",
            "road_name": "충대로",
            "underground_yn": "N",
            "region_2depth_name": "청주시 서원구",
            "zone_no": "28644",
            "sub_building_no": "",
            "region_3depth_name": "개신동",
            "main_building_no": "1",
            "address_name": "충북 청주시 서원구 충대로 1",
            "y": "36.63003935314667",
            "x": "127.45479196889647",
            "region_1depth_name": "충북",
            "building_name": "충북대학교"
          },
          "address_name": "충북 청주시 서원구 충대로 1",
          "address": {
            "b_code": "4311210900",
            "region_3depth_h_name": "성화.개신.죽림동",
            "main_address_no": "12",
            "h_code": "4311259000",
            "region_2depth_name": "청주시 서원구",
            "main_adderss_no": "12",
            "sub_address_no": "",
            "region_3depth_name": "개신동",
            "address_name": "충북 청주시 서원구 개신동 12",
            "y": "36.6284039605616",
            "x": "127.45921419389084",
            "mountain_yn": "N",
            "zip_code": "361763",
            "region_1depth_name": "충북",
            "sub_adderss_no": ""
          },
          "y": "36.63003935314667",
          "x": "127.45479196889647",
          "address_type": "ROAD_ADDR"
        }
      ]
    }
}

and get key "x","y" and those values but, the error is continously occured. how to solve them?

1 Answer 1

3

You should iterate through JSONArray to find JSONObject and then read x and y values.

JsonElement jsonElement = parser.parse(html.toString()).getAsJsonObject();
JsonObject rootObj = jsonElement.getAsJsonObject();
JsonArray documents = (JsonArray) rootObj.get("documents");
Iterator < JsonElement > itr = documents.iterator();
while (itr.hasNext()) {
 JsonObject jsonObject = (JsonObject) itr.next();
 double x = jsonObject.getDouble("x");
 double y = jsonObject.getDouble("y");
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! i solve them!! but, just revise your code. JsonElement jsonElement = parser.parse(html.toString()).getAsJsonObject(); JsonObject rootObj = jsonElement.getAsJsonObject(); JsonArray documents = (JsonArray) rootObj.get("documents"); Iterator<JsonElement> itr = documents.iterator(); while (itr.hasNext()) { JsonObject jsonObject= (JsonObject) itr.next(); JsonElement x = jsonObject.get("x"); JsonElement y = jsonObject.get("y"); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.