How to use the Json module ?
Monkey Forums/Monkey Programming/How to use the Json module ?
| ||
All, I have this Json-formatted text, which I get from an HTTP Get request: { "response": { "version":"0.1", "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", "features": { "conditions": 1 } } , "current_observation": { "image": { "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png", "title":"Weather Underground", "link":"http://www.wunderground.com" }, "display_location": { "full":"Rome, Italy", "city":"Rome", "state":"", "state_name":"Italy", "country":"IY", "country_iso3166":"IT", "zip":"00000", "magic":"1", "wmo":"16240", "latitude":"41.90000153", "longitude":"12.47999954", "elevation":"95.00000000" }, "observation_location": { "full":"Rione Monti, Esquilino, Roma, LAZIO", "city":"Rione Monti, Esquilino, Roma", "state":"LAZIO", "country":"ITALIA", "country_iso3166":"IT", "latitude":"41.892521", "longitude":"12.501983", "elevation":"213 ft" }, "estimated": { }, "station_id":"ILAZIORO91", "observation_time":"Last Updated on October 14, 12:47 PM CEST", "observation_time_rfc822":"Wed, 14 Oct 2015 12:47:57 +0200", "observation_epoch":"1444819677", "local_time_rfc822":"Wed, 14 Oct 2015 12:48:33 +0200", "local_epoch":"1444819713", "local_tz_short":"CEST", "local_tz_long":"Europe/Rome", "local_tz_offset":"+0200", "weather":"Scattered Clouds", "temperature_string":"74.5 F (23.6 C)", "temp_f":74.5, "temp_c":23.6, "relative_humidity":"80%", "wind_string":"Calm", "wind_dir":"SE", "wind_degrees":135, "wind_mph":0.0, "wind_gust_mph":0, "wind_kph":0, "wind_gust_kph":0, "pressure_mb":"1009", "pressure_in":"29.80", "pressure_trend":"0", "dewpoint_string":"68 F (20 C)", "dewpoint_f":68, "dewpoint_c":20, "heat_index_string":"NA", "heat_index_f":"NA", "heat_index_c":"NA", "windchill_string":"NA", "windchill_f":"NA", "windchill_c":"NA", "feelslike_string":"74.5 F (23.6 C)", "feelslike_f":"74.5", "feelslike_c":"23.6", "visibility_mi":"6.2", "visibility_km":"10.0", "solarradiation":"--", "UV":"4","precip_1hr_string":"-9999.00 in ( 0 mm)", "precip_1hr_in":"-9999.00", "precip_1hr_metric":" 0", "precip_today_string":"0.00 in (0 mm)", "precip_today_in":"0.00", "precip_today_metric":"0", "icon":"partlycloudy", "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", "forecast_url":"http://www.wunderground.com/global/stations/16240.html", "history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=ILAZIORO91", "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=41.892521,12.501983", "nowcast":"" } } Question, how do I get, for example, the city name - in this case: "Rome" ? I can get all the keys in this way: For Local it:map.Node<String, JsonValue> = Eachin JsonObject( jso.Get("current_observation") ).GetData() Print it.Key Next but I'm stuck with retrieving a value from a simple element, since they are nested. Anyone willing to help me with a working example based on the above json or similar ? Thanks in advance, Sergio |
| ||
Pull the data item directly from the key value pair (using GetItem if I remember correctly, or GetData), and then cast it to a JSONobject. Then you can perform the same operation on that object until you get to the nesting level necessary to pull out the value you need. |
| ||
Thanks Nobuyuki for your reply, but I still don't get it without a code snippet. Anyone willing to help me with a working example code based on the above json or similar ? |
| ||
I've figured it out:Local jso2:JsonObject = New JsonObject(jsonText) Local jsoCurrent_observation:JsonObject = JsonObject( jso2.Get("current_observation") ) Local jsoDisplay_Location:JsonObject = (JsonObject( jsoCurrent_observation.Get("display_location" ))) Local city$ = jsoDisplay_Location.GetString("full" ) Local obsTime$ = jsoCurrent_observation.GetString("local_time_rfc822") Local weather$ = jsoCurrent_observation.GetString("weather") Local temperature$ = jsoCurrent_observation.GetFloat("temp_c") Local relative_humidity$ = jsoCurrent_observation.GetString("relative_humidity") '"temp_c":17.6, '"relative_humidity":"74%", Print "City = " + city Print "Time = " + obsTime Print "Weather = " + weather Print "temperature = " + temperature Print "relative_humidity = " + relative_humidity |