[url]http://blog.objectgraph.com/index.php/2010/02/24/how-to-use-touch-json-and-sbjson/[/url]
[url]http://stackoverflow.com/questions/286087/best-json-library-to-use-when-developing-an-iphone-application[/url]
I will show how to use each of these libraries to decode JSON code in this post.
Touch JSON:
Include the following in your header
#import "CJSONDeserializer.h"
SB JSON:
The thing i like about SBJSON is it adds functions directly into NSString to get NSDictionary or NSArray objects when deserializing JSON strings.
Start with including the following header
Then you can simply use the following code
Please remember stringWithContentsOfURL has been deprecated so I would not recommend using it. We also just include the entire source of the specific JSON library when building our apps.
[url]http://stackoverflow.com/questions/286087/best-json-library-to-use-when-developing-an-iphone-application[/url]
I will show how to use each of these libraries to decode JSON code in this post.
Touch JSON:
Include the following in your header
#import "CJSONDeserializer.h"
NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSData *flickrData=
[NSData dataWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
NSError *theError = nil;
id theObject =
[[CJSONDeserializer deserializer] deserialize:flickrData error:&theError];
NSLog(@"Error: %@", theError);
NSLog(@"Result: %@", theObject);SB JSON:
The thing i like about SBJSON is it adds functions directly into NSString to get NSDictionary or NSArray objects when deserializing JSON strings.
Start with including the following header
#import "JSON.h"
Then you can simply use the following code
NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSString *output=
[NSString stringWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
id theObject= [output JSONValue];
NSLog(@"%@",theObject);
Please remember stringWithContentsOfURL has been deprecated so I would not recommend using it. We also just include the entire source of the specific JSON library when building our apps.
本文介绍了如何使用TouchJSON和SBJSON两个库来解析JSON数据。通过示例展示了如何从URL获取JSON数据并将其转换为Objective-C的对象,如NSDictionary和NSArray。尽管示例中使用了已废弃的方法,但重点在于展示如何进行JSON数据的解析。
130

被折叠的 条评论
为什么被折叠?



