2017-09-16 6 views
0

JSON 배열에서 중첩 된 JSON 객체를 구문 분석하려고합니다. 중첩 된 JSON 객체 내의 모든 객체와 키 및 값에 일반적인 방법으로 액세스하는 방법을 모르겠습니다.JSON 배열 내의 중첩 JSON 객체

 { 
     "flights": [ 
     { 
      "ident": "AWI4207", 
      "faFlightID": "AWI4207-1505297520-schedule-0000", 
      "origin": { 
       "code": "KORF", 
       "city": "Norfolk, VA", 
       "alternate_ident": "", 
       "airport_name": "Norfolk Intl" 
      }, 
      "destination": { 
       "code": "KPHL", 
       "city": "Philadelphia, PA", 
       "alternate_ident": "", 
       "airport_name": "Philadelphia Intl" 
      }, 
      "filed_ete": 2400, 
      "filed_departure_time": { 
       "epoch": 1505470320, 
       "tz": "EDT", 
       "dow": "Friday", 
       "time": "06:12AM", 
       "date": "15/09/2017", 
       "localtime": 1505455920 
      }, 
     } 
     ] 
    } 

여기까지 내가 한 것입니다.

JSONParser parser = new JSONParser(); 
JSONObject object = (JSONObject) parser.parse(new FileReader("PathToFile.json")); 

JSONArray jsonArray = (JSONArray) object.get("flights"); 
Iterator<Object> eventJSONIterator = jsonArray.iterator(); 

while (eventJSONIterator.hasNext()) { 
    JSONObject jsonEvent = (JSONObject) eventJSONIterator.next(); 
    System.out.println(jsonEvent.toString()); 
} 

개체에는 액세스 할 수 있지만 하위 개체에는 별도로 액세스 할 수 없습니다. "flight"배열의 객체를 반복 할 수있는 방법이 있습니까? JSON 객체 (예 : "origin")가 다시 발생하고 그 안에 루프가 발생했는지 어떻게 알 수 있습니까? 오히려 또한 다음

JSONObject mainObject = (JSONObject) jsonArray.get(0); 

JSONObject origin = (JSONObject) mainObject.get("origin"); 
JSONObject destination = (JSONObject) mainObject.get("destination"); 
JSONObject filed_departure_time = (JSONObject) mainObject.get("filed_departure_time"); 

하는 것보다, 내가 그것을에 속한 개체를 알 수 있도록 DESTINATION_CITY ORIGIN_CODE, ORIGIN_CITY 및 DESTINATION_CODE 등으로 키를 액세스하려는.

+0

을 시도 할 수 있습니다 할 수있는 자바라고 생각합니다. 귀하의 질문에 그대로 태그하십시오. – trincot

답변

1

JSONParser parser = new JSONParser(); 
    JSONObject object = (JSONObject) parser.parse(new FileReader("PathToFile.json")); 

    JSONArray jsonArray = (JSONArray) object.get("flights"); 
    for(int i=0;i<jsonArray.size();i++){ 
     JSONObject flightJSON = jsonArray.getJSONObject(i); 
     JSONObject origin = flightJSON.getJSONObject("origin"); 
     String code = origin.get("code"); 
     String city = origin.get("city"); 
     String alternate_ident = origin.get("alternate_ident"); 
     String airport_name = origin.get("airport_name"); 
     //Similaryly for these 
     JSONObject destination = flightJSON.getJSONObject("destination"); 
     JSONObject filed_departure_time = flightJSON.getJSONObject("filed_departure_time"); 


    } 

당신은 그것을 구문 분석 구조를 알 필요가 있지만, 배열, 객체 또는 다른 엔티티인지 당신이 모르는 경우, 당신은

Object object = json.get("key"); 
    if (object instanceof JSONArray) { 
    // JSONObject 
    jsonArray = (JSONArray)object; 
    }else if (object instanceof JSONObject) { 
    // JSONArray 
    jsonObject = (JSONObject)object; 
    }else { 
    // String or Integer etc. 
} 
+0

for 루프 내의 jsonArray.length()가 어떤 이유로 든 오류를 표시합니다. "메서드 길이()는 JSONArray 유형에 대해 정의되지 않았습니다." – Alohamora153

+0

JSONArray에 대해 org.json.JSONArray를 가져 왔습니까? –

+1

업데이트 된 답변 jsonArray.size()를 참조하십시오. –