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 등으로 키를 액세스하려는.
을 시도 할 수 있습니다 할 수있는 자바라고 생각합니다. 귀하의 질문에 그대로 태그하십시오. – trincot