[英]How can i deserialize this JSON
這是我要解析的JSON數據,但在反序列化方面還是一個新手,我想從此JSON數據中獲取以下屬性:
1. AirportCode 。
2. 拉圖杜德 。
3. 經度 。
這是JSON,其中包含兩個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
}
}
}
然后我的Java代碼 :編輯:
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數據?
你沒有把機場的對象。 機場內部對象是機場數組,您可以直接訪問它。 因此拋出錯誤。
JSONObject obj2 = obj.getJSONObject("Airports");
現在從obj獲取Json Array
JsonArray jsonarray= obj2.getJsonArray("Airport");
現在在jsonarray的長度上使用for循環。
這將使您入門。
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
}
}
輸出將是:
城市代碼:AAL
城市代碼:AAR
我建議使用此工具來正確查看和格式化JSON。
從這里您可以輕松解決問題
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.