JSON API iphone
I need plenty of help in hanlding the JSON respond I will be getting through a web service API. It's forward geo-coding.
How do I get the number of results that was returned to me? Also getting the the value of the result, say the 'address'.
The respond JSON data is as follow. Thanks a lot.
[
{"total":63},{
"t":"1",
"lable":"Gek Poh Shopping Centre",
"address":"762 Jurong West Street 75. (S)640762",
"street":"Jurong West Street 75",
"zip":"640762",
"long":"103.6980151847",
"lat":"1.348986165348",
"x":"355149.0357","y":
"149142.5301",
"is_prem":"0",
"pid":"47120",
"aid":"115810",
"lid":"245690",
"has_biz":"1",
"is_main_building":"1",
"id":"245690",
"cat_id":"80"
},
{
"t":"1",
"lable":"Gek Poh Ville Community Club (C开发者_JAVA技巧C)",
"address":"1 Jurong West Street 74. (S)649149",
"street":"Jurong West Street 74",
"zip":"649149",
"long":"103.69890252806",
"lat":"1.3489703630875",
"x":"355247.7723",
"y":"149140.7302",
"is_prem":"0",
"pid":"2979",
"aid":"116734",
"lid":"127311",
"has_biz":"1",
"is_main_building":"1",
"id":"127311",
"cat_id":"14"
}
]
There's SBJson just download it and copy all the files in SBJson Group to your project and include "SBJson.h"
If your JSON data is in a NSString (dataString), you can do this:
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *dataArray = [parser objectWithString:dataString];
for (NSDictionary *childDic in dataArray) {
NSString *str = [childDic objectForKey:@"address"]; // Here you will get the address
}
Just a sample, Hope it helps
JSON is quite simple do deal with, once you get the hang of it. There are several JSON packages for iPhone, SBjson being one of the more popular ones.
JSON consists of values
, objects
, and arrays
. In your example you have an array (denoted by the []
brackets) containing several objects (denoted by the {}
brackets). Each object, in turn, contains several name/value pairs. With most iPhone JSON parsers the arrays will translate into NSArray objects and the objects will translate into NSDictionary objects. Individual values (not surrounded by any brackets) come through as NSString (for quoted values) or NSNumber (for numerical values not within quotes).
Once you have the result of a JSON parse you "peel the onion" a layer at a time to find your data.
To find out which type you have at any layer use isKindOfClass:
if ( [anObject isKindOfClass:[NSDictionary class]] ) ...
精彩评论