3

How can I navigate JSON string from one key to another nested key and get the value? I have the following string

{ "data" : { "current_condition" : [ { "cloudcover" : "75",
            "humidity" : "29",
            "observation_time" : "07:59 PM",
            "precipMM" : "0.0",
            "pressure" : "1011",
            "temp_C" : "19",
            "temp_F" : "67",
            "visibility" : "16",
            "weatherCode" : "116",
            "weatherDesc" : [ { "value" : "Partly Cloudy" } ],
            "weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" } ],
            "winddir16Point" : "N",
            "winddirDegree" : "350",
            "windspeedKmph" : "26",
            "windspeedMiles" : "16"
          } ],
      "request" : [ { "query" : "01801",
            "type" : "Zipcode"
          } ],
      "weather" : [ { "date" : "2011-05-09",
            "precipMM" : "0.0",
            "tempMaxC" : "19",
            "tempMaxF" : "65",
            "tempMinC" : "10",
            "tempMinF" : "50",
            "weatherCode" : "113",
            "weatherDesc" : [ { "value" : "Sunny" } ],
            "weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0001_sunny.png" } ],
            "winddir16Point" : "NNW",
            "winddirDegree" : "348",
            "winddirection" : "NNW",
            "windspeedKmph" : "24",
            "windspeedMiles" : "15"
          },
          { "date" : "2011-05-10",
            "precipMM" : "0.1",
            "tempMaxC" : "13",
            "tempMaxF" : "56",
            "tempMinC" : "12",
            "tempMinF" : "53",
            "weatherCode" : "122",
            "weatherDesc" : [ { "value" : "Overcast" } ],
            "weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png" } ],
            "winddir16Point" : "NNE",
            "winddirDegree" : "12",
            "winddirection" : "NNE",
            "windspeedKmph" : "31",
            "windspeedMiles" : "19"
          }
        ]
    } }

So I answer my own question: In case someone else want to get value quickly: This is what I was looking for.

JSONObject j = new JSONObject(strResponse);

String weatherDesc = jObject.getJSONObject("data").getJSONArray("weather").getJSONObject(0).getJSONAr­ray("weatherDesc").getJSONObject(0).getString("value");
Gabe
  • 84,912
  • 12
  • 139
  • 238
snail
  • 151
  • 3
  • 9
  • 2
    You haven't specified what language you are using. Look halfway down this page to find a solution for which ever one you are using. http://json.org/ – Gazler May 09 '11 at 21:36
  • 1
    Given the OP's post history, I'll do a bet on C#. Or do you want to do this in JavaScript? – BalusC May 09 '11 at 21:37
  • @BalusC: but then again, he's writing web services in .net and one question about consuming web services on android...sooooo... – Shawn Mclean May 09 '11 at 21:38
  • Gazler, I clicked on two links already in that, I was expecting something of type xpath navigation where I put xyz/abc/ and find the value. Seems it will take sometime for me learn this. I want to get value of "data:"weather": "date": "2011-05-09" : tempMaxC ". where date is first node. – snail May 09 '11 at 21:51
  • http://www.json-generator.com/j/bQFuLJYZMy?indent=4 i cant parse this link,,it shows similar pattern to your lnk – Denny Mathew Feb 27 '14 at 07:06
  • Don't edit the solution into your question. Answer your own question instead. – The SE I loved is dead Nov 03 '16 at 23:15

2 Answers2

3

There are JSON libraries in pretty much any language. If you suggest one, I might be able to point you to something.

In the meantime, here are a few:

And so on. I suggest a quick google for the language of your choice.

Community
  • 1
  • 1
Dave Orr
  • 1,082
  • 1
  • 9
  • 18
  • thanks, sorry I did not mention it is Java and Android 1.5. I am using eclipse as IDE. I looked google's Gson and Json but could not find any sample to find a node and get the value like I do in C# dataset or xpath – snail May 09 '11 at 22:11
  • @webservicenewbie The last link, the Android one, should be the documentation you're looking for. – Dave Orr May 09 '11 at 22:14
  • Indeed it's trivial with Android's JSONObject, has the OP not looked at the docs? – David Snabel-Caunt May 09 '11 at 22:44
  • Excellent, David Caunt! Thanks Dave Orr. – snail May 09 '11 at 23:18
1

Generally speaking you will use a library that is already built specifically for you language, what language are you trying to read the data in? Many languages have a couple of libraries available, some languages may have built in support, like JavaScript.

if you just need to understand the data, it is pretty readable...

Grady Player
  • 14,399
  • 2
  • 48
  • 76