0

I try to make a simple weather app and for that I use YahooWeather api. So I get a response something like this:

/**/yqlCallback({
    "query": {
        "count": 3,
        "results": {
            "channel": [{
                "item": {
                    "pubDate": "Fri, 16 Jan 2015 11:00 am EET",
                    "condition": {
                        "temp": "1",
                        "text": "Cloudy"
                    },
                }
            },
            {
                "item": {
                    "title": "1",

                }
            },
            {
                "item": {
                    "title": "2",

                }

            }]
        }
    }
});

The only data's I need from here is pubDate, temp and text. So here is how I try to get those values.

    JSONObject main = json.getJSONObject("query").getJSONObject("result").getJSONArray("channel")
    .getJSONObject(0);
    JSONObject details = main.getJSONObject("condition");
String t1 = details.getString("text");
String t2 = details.getString("temp");
String t3 = main.getString("pubDate");

This give's me this error: One or more fields not found in the JSON data.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
Zara Gheorghe
  • 488
  • 4
  • 13

2 Answers2

0

In current json no JSONObject present with result key. Use results instead of result to get JSONObejct from query JSONObject:

JSONObject main = json.getJSONObject("query").
                getJSONObject("results").getJSONArray("channel");
JSONObject items = main.getJSONObject("item");
JSONObject details = items.getJSONObject("condition");
String t1 = details.getString("text");
String t2 = details.getString("temp");
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • @WdarinS: see my edit answer `condition` object is in `item` jsonObject so you first need to get `item` object from main then get `condition` from it – ρяσѕρєя K Jan 16 '15 at 12:55
0

The problem was that I needed to add at the end of .getJSONArray("channel").getJSONObject(0).getJsSONObject("item"); anyway, thanks.

Zara Gheorghe
  • 488
  • 4
  • 13