我该如何反序列化这个JSON

时间:2018-02-01 14:06:15

标签: java android json

这是我要解析的 JSON 数据,但在反序列化方面仍然是新手,我想从 JSON数据获取以下属性:

1 AIRPORTCODE

2 Latutude

3 经度

这是包含两个机场列表的 JSON

{
    "AirportResource": {
        "Airports": {
            "Airport": [{
                "AirportCode": "AAL",
                "Position": {
                    "Coordinate": {
                        "Latitude": 57.09305556,
                        "Longitude": 9.85
                    }
                },
                "CityCode": "AAL",
                "CountryCode": "DK",
                "LocationType": "Airport",
                "Names": {
                    "Name": [{
                        "@LanguageCode": "xx",
                        "$": "Aalborg"
                    }, {
                        "@LanguageCode": "de",
                        "$": "Aalborg"
                    }, {
                        "@LanguageCode": "ru",
                        "$": "Ольборг"
                    }, {
                        "@LanguageCode": "pt",
                        "$": "Aalborg"
                    }, {
                        "@LanguageCode": "jp",
                        "$": "オールボア"
                    }, {
                        "@LanguageCode": "kr",
                        "$": "올보르그"
                    }, {
                        "@LanguageCode": "en",
                        "$": "Aalborg"
                    }, {
                        "@LanguageCode": "it",
                        "$": "Aalborg"
                    }, {
                        "@LanguageCode": "fr",
                        "$": "Aalborg"
                    }, {
                        "@LanguageCode": "es",
                        "$": "Aalborg"
                    }, {
                        "@LanguageCode": "ka",
                        "$": "奧爾堡"
                    }, {
                        "@LanguageCode": "pl",
                        "$": "Aalbork"
                    }, {
                        "@LanguageCode": "mi",
                        "$": "奥尔堡"
                    }]
                },
                "UtcOffset": 2,
                "TimeZoneId": "Europe\/Copenhagen"
            }, {
                "AirportCode": "AAR",
                "Position": {
                    "Coordinate": {
                        "Latitude": 56.30388889,
                        "Longitude": 10.62
                    }
                },
                "CityCode": "AAR",
                "CountryCode": "DK",
                "LocationType": "Airport",
                "Names": {
                    "Name": [{
                        "@LanguageCode": "xx",
                        "$": "Aarhus"
                    }, {
                        "@LanguageCode": "en",
                        "$": "Aarhus"
                    }, {
                        "@LanguageCode": "de",
                        "$": "Aarhus"
                    }, {
                        "@LanguageCode": "it",
                        "$": "Aarhus"
                    }, {
                        "@LanguageCode": "fr",
                        "$": "Aarhus"
                    }, {
                        "@LanguageCode": "es",
                        "$": "Aarhus"
                    }]
                },
                "UtcOffset": 2,
                "TimeZoneId": "Europe\/Copenhagen"
            }]
        },
        "Meta": {
            "@Version": "1.0.0",
            "Link": [{
                "@Href": "https:\/\/api.lufthansa.com\/v1\/references\/airports\/?limit=2&LHoperated=0&offset=0",
                "@Rel": "self"
            }, {
                "@Href": "https:\/\/api.lufthansa.com\/v1\/references\/airports\/?limit=2&LHoperated=0&offset=2",
                "@Rel": "next"
            }, {
                "@Href": "https:\/\/api.lufthansa.com\/v1\/references\/airports\/?limit=2&LHoperated=0&offset=1260",
                "@Rel": "last"
            }, {
                "@Href": "https:\/\/api.lufthansa.com\/v1\/references\/cities\/{cityCode}",
                "@Rel": "related"
            }, {
                "@Href": "https:\/\/api.lufthansa.com\/v1\/references\/countries\/{countryCode}",
                "@Rel": "related"
            }],
            "TotalCount": 1261
        }
    }
}

然后我的 Java co <​​strong> de: EDITTED:

private void parseJson(String  result)
    {
        try {
            if (result!=null) {
JSONObject obj = new JSONObject(result).getJSONObject("AirportResource").getJSONObject("Airports");
                JSONArray arr = obj.getJSONArray("Airport");
                for (int i = 0; i < arr.length(); i++)
                {
                    String AirportCode = arr.getJSONObject(i).getString("AAL");
                    String Latitude = arr.getJSONObject(i).getJSONObject("Position").getJSONObject("Coordinate").getString("Latitude");
                    String Longitude =  arr.getJSONObject(i).getJSONObject("Position").getJSONObject("Coordinate").getString("Longitude");
                    System.out.println("Airport : " + AirportCode + " Latitude:  " + Latitude+
                            " Longitude : " + Longitude );
                }

            }
            else{
                Toast.makeText(MainActivity.this,Config.POOR_NETWORK_CONNECTION, Toast.LENGTH_LONG).show();

            }
        }
        catch (JSONException r){
            System.out.println("ERROR PROB : "+  r);

        }
    }

我得到的例外是:

org.json.JSONException: No value for AirportResource

我如何解析这种JSON类型的数据?

3 个答案:

答案 0 :(得分:1)

你没有接受机场的目标。 Inside Airports对象是Airport阵列,您可以直接访问它。因此抛出错误。

public sum = (total, currentValue) => total + currentValue;

现在从obj

获取Json数组
JSONObject obj2 = obj.getJSONObject("Airports");

现在在jsonarray的长度上使用for循环。

答案 1 :(得分:1)

这一定会让你开始。

private static void parseJson(JSONObject object) throws JSONException {
    if (object!=null) {
        JSONObject airportResource = object.getJSONObject("AirportResource");
        JSONObject airports = airportResource.getJSONObject("Airports");
        JSONArray airportArray = airports.getJSONArray("Airport");

        for (int i = 0 ; i < airportArray.length(); i++) {
            JSONObject airport = (JSONObject) airportArray.get(i);

            //System.out.println(airport);

            System.out.println("CityCode: " + airport.getString("CityCode"));
        }

    }
    else{
        //null

    }
}

输出将是:

  

CityCode:AAL

     

CityCode:AAR

答案 2 :(得分:-1)

我建议使用此tool来正确查看和合成您的JSON。

这是结果enter image description here

从这里您可以轻松解决您的问题

JSONObject AirportResource= obj.getJSONObject("AirportResource");
JsonArray AirPorts= AirportResource.getJSONObject("Airports");
JsonArray jsonarray= AirPorts.getJsonArray("Airport");

      for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject obj= AirportResource.get(i); do sth with it
        obj.getString("key"); //do sth with it
       }