ios - SBJson displays null -


i trying parse json data sbjson show current temperature. example code tutorial works perfect: tutorial: fetch , parse json

when change code json feed null. kind of new json followed every tutorial , documentation found. json source used: json source

my code sbjson:

nsstring *responsestring = [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding]; self.responsedata = nil;  nsarray* currentw = [(nsdictionary*)[responsestring jsonvalue] objectforkey:@"current_weather"];  //choose random loan nsdictionary* weathernow = [currentw objectatindex:0];  //fetch data nsnumber* tempc = [weathernow objectforkey:@"temp_c"]; nsnumber* weathercode = [weathernow objectforkey:@"weathercode"];   nslog(@"%@ %@", tempc, weathercode); 

and of course have implemented other sbjson code.

there no current_weather key in json data posted. structure is:

{ "data": { "current_condition": [ { ..., "temp_c": "7", ... } ], ... } } 

here's visual representation:

json visual representation

therefore, temp_c, you'd need first obtain top-level data property:

nsdictionary* json = (nsdictionary*)[responsestring jsonvalue]; nsdictionary* data = [json objectforkey:@"data"]; 

then, that, obtain current_location property:

nsarray* current_condition = [data objectforkey:@"current_condition"]; 

and finally, current_location array, element you're interested in:

nsdictionary* weathernow = [current_condition objectatindex:0]; 

also note temp_c , weathercode strings, not numbers. transform them numbers, instead of:

nsnumber* tempc = [weathernow objectforkey:@"temp_c"]; nsnumber* weathercode = [weathernow objectforkey:@"weathercode"]; 

you use like:

int tempc = [[weathernow objectforkey:@"temp_c"] intvalue]; int weathercode = [[weathernow objectforkey:@"weathercode"] intvalue]; 

(or floatvalue / doublevalue if value not supposed int, rather float or double)

you use %d (or %f float / double) format string:

nslog(@"%d %d", tempc, weathercode); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -