0

I'm relatively new to Android and I'm trying to parse some data and I seem to be getting the following error in LogCat:

Unexpected value from nativeGetEnabledTags: 0
Error parsing data org.json.JSONException

I'm getting my JSON data from: http://api.wunderground.com/api/180dde448747af27/forecast/q/UK/Bradford.json

I'm using the following code to pull out data:

JSONArray forecasts = json.getJSONArray("forecast");

for (int i=0;i<forecasts.length();i++) {                        
    HashMap<String, String> map = new HashMap<String, String>();    
    JSONObject e = forecasts.getJSONObject(i);

    // simpleforecast is a json array
    JSONArray forecastday = json.getJSONArray("forecastday");
    JSONObject fd = forecastday.getJSONObject(i);

    String icon = fd.getString("icon");

    map.put("id", String.valueOf(i));
    map.put("icon", icon);
    mylist.add(map);            
}

I believe the error is something to do with my JSON pulling, I've probably not parsed it correctly, but I can't seem to find the correct way to get it out. This code is surrounded by a try-catch and then I've got a list adapter that I add the code to.

Apologies if I've missed anything but I'm fairly confident this is sufficient as I believe this is where the error is coming from.

2
  • 1
    If you don't catch the exception a line number should show up in LogCat and then you know which statement is causing it, should narrow your search a fair bit. Commented Mar 9, 2013 at 22:51
  • 2
    forecast isn't an array, use getJSONObject instead. Commented Mar 9, 2013 at 23:11

2 Answers 2

1

parse current json string as to get icon from forecastday JSONArray :

// get Json forecast object
JSONObject forecasts_obj = json.getJSONObject("forecast");
// get Json simpleforecast object
JSONObject simpleforecast_obj = forecasts_obj.getJSONObject("simpleforecast");

// get Json forecastday Array
JSONArray forecastday_arr = simpleforecast_obj.getJSONArray("forecastday");

for (int i=0;i<forecastday_arr.length();i++) {                        
    HashMap<String, String> map = new HashMap<String, String>();    
    JSONObject e = forecastday_arr.getJSONObject(i);

    String icon = e.getString("icon");
    map.put("id", String.valueOf(i));
    map.put("icon", icon);
    mylist.add(map);            
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this, it explains a lot and I've now got it working. Much appreciated.
1

You misidentify forcast as a JSONArray.

JSON

So you should not using JSONArray forecasts = json.getJSONArray("forecast"); to parse forecast, you should use JSONObject forecastObj = json.getJSONObject("forecast"). However, forecastday is a json array, so you can use forecastObj.getJSONArray("forecastday"), and then work on it.

1 Comment

Thank you for your help, it was very informative. I've now got it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.