iOS returns “null” data from JSON feed

I am downloading and parsing a JSON feed in my iOS application. Some parts of the data that the JSON feed provides are shown correctly in my iOS application, however some parts of the JSON feed seem to return "null" even though in the actual JSON feed itself, they do not contain the data "null".

Here is my code:

NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves  error:&myError];
NSArray *results = [res objectForKey:@"current_observation"];
NSArray *cur = [results valueForKey:@"weather"];
NSArray *loc = [results valueForKey:@"full"];
NSArray *tmp = [results valueForKey:tmptype];

The JSON feed I am trying to load is this one: http://api.wunderground.com/api/595007cb79ada1b1/conditions/q/CA/San_Francisco.json

One part of the JSON feed that returns "null" is:

"full":"San Francisco, CA"

The above returns "null".... why? And how do I go about fixing this issue?

Thanks, Dan

Answers


you need

NSString *full= [[results valueForKey:@"display_location"] valueForKey:@"full"]];

because it is a nested JSON.


First of the results are not of type NSArray.

When you are trying to get the value fo the key: full you are not in the correct path. You are in current_observation.full but the key you want is in current_observation.display_location.full. Since the JSON is nested you will need go in to the correct path.

You can get the full path in one go:

NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError];
NSString *weather = [res valueForKeyPath:@"current_observation.weather"];
NSString *cur = [res valueForKeyPath:@"current_observation.display_location.full"];

NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves  error:&myError];
NSArray *results = [res objectForKey:@"current_observation"];
NSArray *cur = [results valueForKey:@"weather"];
NSString *full= [[res valueForKey:@"display_location"] valueForKey:@"full"]];
NSArray *tmp = [results valueForKey:tmptype];

try this it may help you.


Need Your Help

IISNode exec silently failing

node.js iis exec windows-server-2012 iisnode

I'm trying to build a automatic code pulling service with GitHub webhooks and node.js.

Android webkit bug: white screen flashed (for < 1 second) when changing webview URLs

android webview

Bug report: http://code.google.com/p/android/issues/detail?id=25722

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.