繁体   English   中英

在Android中解析复杂的Json对象

[英]Parsing Complex Json Object in android

我想从json对象中检索以下文件

从OriginLocation = >> CityCode,DepartureDate,DepartureTime

从DestinationLocation = >> CityCode,ArrivalTime,ArrivalDate

从票价= >> OrigTotalFareAmt

从FlightDetails == >> CabinClassCode,JourneyDuration

使用一个for循环

http://pastie.org/8563070#7

尝试这个。

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