We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have been trying for about six hours now to integrate weather updates in my code. I got an API key from wunderground and all, and I can even print the json to the console as a string. My problem is that I want to be able to access a certain part of the update, not just print all that nonsense. I've been working with v2.0b7 because it works with the other 900+ lines of my program, and 2.1.1 does not. It seems that 2.1.1 brought some new JSON stuff, so I am isolating the problem and making a very simple program in 2.1.1 to:
I'd like to get it to a point where temperature, cloud cover, humidity etc. live in strings. The raw .json looks like this:
HTTP/1.1 200 OK
X-CreationTime:
0.065
Date:
Sun, 26 Jan 2014 18:24:40 GMT
Content-Length:
2647
Expires:
Sun, 26 Jan 2014 18:24:40 GMT
Last-Modified:
Sun, 26 Jan 2014 18:24:40 GMT
Set-Cookie:
DT=1390760680:18375:365-d6; path=/; expires=Fri, 01-Jan-2020 00:00:00 GMT; domain=.wunderground.com
Content-Type:
application/json; charset=UTF-8
Server:
Apache/1.3.42 (Unix) PHP/5.3.2
Pragma:
no-cache
Cache-Control:
max-age=0, no-cache
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
},
"current_observation": {
"image": {
"url": "http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "San Francisco, CA",
"city": "San Francisco",
"state": "CA",
"state_name": "California",
"country": "US",
"country_iso3166": "US",
"zip": "94101",
"magic": "1",
"wmo": "99999",
"latitude": "37.77500916",
"longitude": "-122.41825867",
"elevation": "47.00000000"
},
"observation_location": {
"full": "SOMA - Near Van Ness, San Francisco, California",
"city": "SOMA - Near Van Ness, San Francisco",
"state": "California",
"country": "US",
"country_iso3166": "US",
"latitude": "37.773285",
"longitude": "-122.417725",
"elevation": "49 ft"
},
"estimated": {},
"station_id": "KCASANFR58",
"observation_time": "Last Updated on January 26, 10:23 AM PST",
"observation_time_rfc822": "Sun, 26 Jan 2014 10:23:56 -0800",
"observation_epoch": "1390760636",
"local_time_rfc822": "Sun, 26 Jan 2014 10:24:40 -0800",
"local_epoch": "1390760680",
"local_tz_short": "PST",
"local_tz_long": "America/Los_Angeles",
"local_tz_offset": "-0800",
"weather": "Mostly Cloudy",
"temperature_string": "62.0 F (16.7 C)",
"temp_f": 62,
"temp_c": 16.7,
"relative_humidity": "52%",
"wind_string": "From the NW at 1.0 MPH",
"wind_dir": "NW",
"wind_degrees": 322,
"wind_mph": 1,
"wind_gust_mph": 0,
"wind_kph": 1.6,
"wind_gust_kph": 0,
"pressure_mb": "1020",
"pressure_in": "30.14",
"pressure_trend": "+",
"dewpoint_string": "44 F (7 C)",
"dewpoint_f": 44,
"dewpoint_c": 7,
"heat_index_string": "NA",
"heat_index_f": "NA",
"heat_index_c": "NA",
"windchill_string": "NA",
"windchill_f": "NA",
"windchill_c": "NA",
"feelslike_string": "62.0 F (16.7 C)",
"feelslike_f": "62.0",
"feelslike_c": "16.7",
"visibility_mi": "10.0",
"visibility_km": "16.1",
"solarradiation": "--",
"UV": "1",
"precip_1hr_string": "0.00 in ( 0 mm)",
"precip_1hr_in": "0.00",
"precip_1hr_metric": " 0",
"precip_today_string": "0.00 in (0 mm)",
"precip_today_in": "0.00",
"precip_today_metric": "0",
"icon": "mostlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
"forecast_url": "http://www.wunderground.com/US/CA/San_Francisco.html",
"history_url": "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KCASANFR58",
"ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.773285,-122.417725"
}
}
By the way, I think the nested objects may be what is throwing me off here, and I found this page which explains how to access them. May or may not be helpful but here it is. This can likely be solved in about 4 lines of code, but I'm striking out. If someone can make it work, I'd be grateful. Even more so if it can be made to work in 2.0b7. Thanks for your help!
Answers
when I try this, I get "A JSON object text must begin with { } "
from
json = loadJSONObject("test1.JSON");
maybe you need to.... cut it till the 1st {
?
hmmm....
I needed to totally strip your data down to
so apart from the lines before the 1st {
there where other issues : processing kept saying can't load, expect , or }
and I stripped it more and more until this error stopped.
I think it has to do with one of the urls... maybe because of space or because of \ or \
You need to check the whole file and strip it till it starts working (or add lines to my stripped down version till it stops working)
also space could be an issue
this is p2.1
for a working version also see
http://forum.processing.org/two/discussion/1316/small-json-retrieve-of-a-digital-library-meta-data-only#Item_9
Chrisir, the first lines of the give code are actually just the header of the HTTP response. Ignore them, indeed. And the forum messes URLs in code, so you have to strip out the a href parts... For example, the last lines should be:
(without the spaces I have put after : to avoid this issue...)
Thank you!
He needs to do that programmically, so load the whole file into a String, strip everything before the 1st { and then strip out the a href parts
and then put it into a JSONobject as in my code......
sounds like hard work...
Sorry I haven'd had a chance to get back to this post very quickly. @Chrisir I can get the file to the string, but I wasn't sure about how to strip the data correctly. If I did something like
String strippedWeather = rawWeather.replace("unnecessary stuff", "");
I'd never be able to strip out the excess because it changes all the time. I'm learning a to properly use all the string functions still, but is there a way I can do as you say and remove everything before the 1st { ? The replace thing is way too specific I think, but I'm not sure besides that.FYI as an alternative to this, I found a NWS plain text webpage that contains the week's forecast. I simply imported the html to a string and .replace()'d all the html stuff into just the forecast. It took AGES to manipulate the string to do that. Since I have this (semi) working, is there any point in using the JSON? If it won't be any better or easier, I'm going to forget it :) Since it was so hard to get the html working from even a simple page, is there a better way to do that? If I want to pull just the temperature, for example, can I have it pull just that part of the html? Is there a way to navigate the tags as there is with XML? Thanks for your help!
First question, you can use:
String strippedWeather = rawWeather.substring(rawWeather.indexOf("{"));
Second question, I can highly recommend:
http://jsoup.org/
yes, jsoup sounds great
Thank you! When I get back to playing with JSON I will give that method a try.