简体   繁体   中英

How can i deserialize this JSON

This is my JSON data i want to parse but am still new in deserialization, i want to get the following Attributes from this JSON data :

1. AirportCode .

2. Latutude .

3. Longitude .

This is the JSON containing two lists of the Airports.

{
    "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
        }
    }
}

Then my Java co 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);

        }
    }

The exception am getting is :

org.json.JSONException: No value for AirportResource

How can i parse this JSON kind of Data?

You didnt take the object of Airports. Inside Airports object is the Airport array and you are directly accessing it. Hence throwing error.

JSONObject obj2 = obj.getJSONObject("Airports");

Now get Json Array from obj

JsonArray jsonarray= obj2.getJsonArray("Airport");

Use the for loop on jsonarray's length now.

This will HOPEFULLY get you started.

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

    }
}

The output will be:

CityCode: AAL

CityCode: AAR

I recommend using this tool to view and formate your JSON properly.

this is the result 在此处输入图片说明

from here you could easily resolve your problem

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
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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