how-to-use-touch-json-and-sbjson/

本文介绍了如何使用TouchJSON和SBJSON两个库来解析JSON数据。通过示例展示了如何从URL获取JSON数据并将其转换为Objective-C的对象,如NSDictionary和NSArray。尽管示例中使用了已废弃的方法,但重点在于展示如何进行JSON数据的解析。
[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"

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.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值