开发者

need a Sample JSON parser in objective c

开发者 https://www.devze.com 2023-01-22 16:08 出处:网络
I want to know to parse a json object in objecti开发者_如何学Cve C. I got the JSON object by loading a url. Can u please tell me how to do it or any samples or any reference.

I want to know to parse a json object in objecti开发者_如何学Cve C. I got the JSON object by loading a url. Can u please tell me how to do it or any samples or any reference.

The following is the sample json.

{
"name":"WFNX",
"now":
     {
     "id":"17749528",
     "song":"Back Down South",
     "artist":"Kings Of Leon"
     },
"desc":"101.7 - True Alternative",
"audiostream":"http:\/\/www.streamaudio.com\/stations   \/asx\/wfnx_fm.asx",
"genre":"Rock",
"tz":"EST",
"id":"17880",
"yes":"station"
}


Checkout this JSON framework for Objective-C on code.google.com or on Github.

It is pretty straightforward to use. You instantiate your SBJSON object then call objectWithString with your buffer:

SBJSON * parser = [[SBJSON alloc] init];
NSString * buffer = @"{"name":"WFNX"}";
NSError* error = NULL;
// the type of json will depend on the JSON data in buffer, in this case, it will be 
// and NSDictionary with one key/value pair "name"->"WFNX"
id json = [parser objectWithString:buffer error:&error];


There are a few out there, see:

http://code.google.com/p/json-framework/
http://github.com/schwa/TouchJSON

for a big list:

http://www.json.org/

is a great reference site for JSON


I'm using YAJLiOS parser,not bad and compatable with ARC, here's documentation

http://gabriel.github.com/yajl-objc/

and parser itself on github

https://github.com/gabriel/yajl-objc

ex.

   NSData *JSONData = [NSData dataWithContentsOfFile:@"someJson.json"];
   NSDictionary *JSONDictionary = [tempContainer yajl_JSON];

and getting objects by objectForKey method or valueFoKey method if you need an array

0

精彩评论

暂无评论...
验证码 换一张
取 消