简体   繁体   English

在Android中解析复杂的Json对象

[英]Parsing Complex Json Object in android

i want to retrieve following filed from json object 我想从json对象中检索以下文件

From OriginLocation =>> CityCode, DepartureDate, DepartureTime 从OriginLocation = >> CityCode,DepartureDate,DepartureTime

From DestinationLocation =>> CityCode, ArrivalTime, ArrivalDate 从DestinationLocation = >> CityCode,ArrivalTime,ArrivalDate

From Fare =>> OrigTotalFareAmt 从票价= >> OrigTotalFareAmt

From FlightDetails ==>> CabinClassCode, JourneyDuration 从FlightDetails == >> CabinClassCode,JourneyDuration

using one for loop 使用一个for循环

http://pastie.org/8563070#7 http://pastie.org/8563070#7

Try this. 尝试这个。

private void parseJson(JSONObject data) {

        if (data != null) {
            Iterator<String> it = data.keys();
            while (it.hasNext()) {
                String key = it.next();
                try {
                    if (data.get(key) instanceof JSONArray) {
                        JSONArray arry = data.getJSONArray(key);
                        int size = arry.length();
                        for (int i = 0; i < size; i++) {
                            parseJson(arry.getJSONObject(i));
                        }
                    } else if (data.get(key) instanceof JSONObject) {
                        parseJson(data.getJSONObject(key));
                    } else {
                        System.out.println("Key :" + key);
                        System.out.println("Value :" + data.getString(key));
                    }
                } catch (Throwable e) {
                    try {
                        System.out.println("Key :" + key);
                        System.out.println("Value :" + data.getString(key));
                    } catch (Exception ee) {
                    }
                    e.printStackTrace();

                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM