Reputation: 1229
i am very new to something called JSON Parsing
in iOS. Can anybody explain me how to parse data from the scratch. Basically what I am tying to tell is that I am using Yahoo weather api for fetching the weather of a location.http://weather.yahooapis.com/forecastjson?w=2502265
is the link that I am using it .Now how can I parse the data from it?
I am getting error by doing this.Can somebody rectify it or tell me how to do it?
NSString * address = @"http://weather.yahooapis.com/forecastjson?w=2502265";
NSString * request = [NSString stringWithFormat:@"%@",address];
NSURL * URL = [NSURL URLWithString:request];
NSString* JSON = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error];
NSError *e = nil;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:JSON options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@", json);
Upvotes: 0
Views: 2265
Reputation: 11607
I have found that AFNetworking has made my life much easier.
I usually do something more than this like pass in POST parameters, but this will do for your case:
NSURL *url = [NSURL URLWithString:@"http://weather.yahooapis.com/forecastjson?w=2502265"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// PARSE YOUR JSON DICTIONARY HERE
[self parseResult:JSON];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Failed to get data from web service, error: %@", [error localizedDescription]);
}];
[operation start];
// method to parse web service JSON
-(void)parseResult:(id)jsonDictionary
{
// parse result here
NSLog(@"Title = %@", [jsonDictionary valueForKey:@"title"];
}
Hope that helps.
What you're doing is requesting information from a web service.
You can think of web service like scripts on the server listening for specific POST or GET parameters.
You might have a web service like this:
http://www.mywebservice.com/rest/datatype=User?name=JohnSmith&age=20
Notice the "datatype", "name" and "age" are the parameters the web service is expecting. When you make a request to the web service, you usually pass in the value (in this case, "User", "JohnSmith" and "20" are the values) to those parameters.
These days, a web service will usually return you the data in the form of JSON or XML.
JSON doesn't have to do all those XML formatting, and as a result, JSON will tend to be more of the preferred choice for returning data.
The JSON data returned will look something like:
{
users = {(
({
name = John Smith;
age = 20;
address = 123 Easy Street EARTH
}),
({
name = Bob Brown;
age = 35;
address = 456 Some Road EARTH
})
)};
}
The above can be a bit intimidating at first but once you deal with it once, you'll realise that these are usually dictionaries nested inside arrays, nested inside giant dictionary.
As in the above case, the returned JSON data is a single giant dictionary containing all users. When you do something like:
[JSON objectForKey:@"users"]
You get the "users" array:
users = {(
({
name = John Smith;
age = 20;
address = 123 Easy Street EARTH
}),
({
name = Bob Brown;
age = 35;
address = 456 Some Road EARTH
})
)};
Then when you want to get a specific user from the "users" array, say Bob Brown, you would do something like:
[[JSON objectForKey:@"users"] objectAtIndex:1]
And that will return you:
{
name = Bob Brown;
age = 35;
address = 456 Some Road EARTH
}
Finally, to get a property of a user such as their name, you can go:
[[[JSON objectForKey:@"users"] objectAtIndex:1] valueForKey:@"name"];
For example:
NSLog(@"User name = %@", [[[JSON objectForKey:@"users"] objectAtIndex:1] valueForKey:@"name"]);
Real web service dictionaries usually aren't that nicely formatted, they're usually a bit more convoluted, especially those CMS like Drupal (run!!!) :D
Upvotes: 2