2016-11-29 102 views
-5

我想運行下面的代碼,但它拋出一個異常。無法轉換爲java中的JSONArray(android)

代碼

try { 
    JSONObject jsonObject = new JSONObject(result);  
    JSONArray jsonArray = jsonObject.getJSONArray("current"); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

主要的例外是由於JSONArray part.If我刪除該部分比所述代碼運行正常。

以下是API:

{ 
"location": { 
    "name": "Paris", 
    "region": "Ile-de-France", 
    "country": "France", 
    "lat": 48.87, 
    "lon": 2.33, 
    "tz_id": "Europe/Paris", 
    "localtime_epoch": 1480463619, 
    "localtime": "2016-11-29 23:53" 
}, 
"current": { 
    "last_updated_epoch": 1480463580, 
    "last_updated": "2016-11-29 23:53", 
    "temp_c": -1, 
    "temp_f": 30.2, 
    "is_day": 0, 
    "condition": { 
     "text": "Clear", 
     "icon": "//cdn.apixu.com/weather/64x64/night/113.png", 
     "code": 1000 
    }, 
    "wind_mph": 0, 
    "wind_kph": 0, 
    "wind_degree": 0, 
    "wind_dir": "N", 
    "pressure_mb": 1033, 
    "pressure_in": 31, 
    "precip_mm": 0, 
    "precip_in": 0, 
    "humidity": 69, 
    "cloud": 0, 
    "feelslike_c": -1, 
    "feelslike_f": 30.2 
} 
} 

異常發生:

W/System.err: org.json.JSONException: Value 
{"last_updated_epoch":1480477541,"last_updated":"2016-11-30 
03:45","temp_c":13,"temp_f":55.4,"is_day":0,"condition":{"text":"Overcast","icon":"\/\/cdn.apixu.com\/weather\/64x64\/night\/122.png","code":1009},"wind_mph":0,"wind_kph":0,"wind_degree":0,"wind_dir":"N","pressure_mb":1016,"pressure_in":30.5,"precip_mm":0,"precip_in":0,"humidity":77,"cloud":0,"feelslike_c":13,"feelslike_f":55.4} 
at current of type org.json.JSONObject cannot be converted to 
JSONArray 

回答

2

在你Jsoncurrent似乎是一個對象而不是數組。因此,改變

jsonObject.getJSONArray("current");

jsonObject.getJSONObject("current");

會修正這個錯誤。

1

'current'不是數組,它是一個JSON對象,因此將其解析爲對象,然後可以從中檢索數據。 您可以使用

JSON Viewer

您JSON的好/可以理解的觀點。

-1

您可以簡單地使用Gson庫來將所有這些json轉換爲對象類..易於獲取和設置,無需一次又一次迭代json。

創建模型類名LocationModel

public class LocationModel 
{ 
    private Location location; 
    private Current current; 

    //create setter and getter 

} 

當前對象創建模型

public class Current 
{ 
    private String temp_f; 
    private Condition condition; 
    private String temp_c; 
    private String wind_degree; 
    private String wind_dir; 
    private String wind_kph; 
    private String is_day; 
    private String pressure_in; 
    private String humidity; 
    private String precip_mm; 
    private String wind_mph; 
    private String pressure_mb; 
    private String feelslike_f; 
    private String cloud; 
    private String last_updated_epoch; 
    private String feelslike_c; 
    private String last_updated; 
    private String precip_in; 

    //create setter and getter 
} 

創建條件 Model類

public class Condition 
{ 
    private String icon; 
    private String text; 
    private String code; 

    //create setter and getter 
} 

創建位置模型類

public class Location 
{ 
    private String region; 
    private String localtime; 
    private String localtime_epoch; 
    private String lon; 
    private String tz_id; 
    private String name; 
    private String lat; 
    private String country; 

    //create setter and getter 
} 

如何使用?

LocationModel locationM = new Gson().fromJson(**YOURJSONSTRING**,LocationModel.class); 

locationM.getLocation().getRegion(); //get Region <br> 
locationM.getCurrent().getCondition().getText(); //get condition text from current<br> 
locationM.getCurrent().getHumidity(); //get current humidity<br> 
相關問題