JSON数据解析
来源:互联网 发布:淘宝客服电话人工接听 编辑:程序博客网 时间:2024/06/03 05:06
JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与客户端的交互,Json语法参考。关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也是比其他第三方开源项目的高很多,详情可看下图。
NSJSONSerialization提供了Json数据封包、Json数据解析,NSJSONSerialization将JSON数据转换为NSDictionary或NSArray解包方法,将NSDictionary、NSArray对象转换为JSON数据(可以通过调用isValidJSONObject来判断NSDictionary、NSArray对象是否可以转换为JSON数 据)封包。
Json数据封包
<span style="font-size:14px;">NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil]; // isValidJSONObject判断对象是否可以构建成json对象 if ([NSJSONSerialization isValidJSONObject:dic]){ NSError *error; // 创造一个json从Data, NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性。 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error]; NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"json data:%@",json); }</span><span style="font-size: 18px;"> </span>
Json数据解析
<span style="font-size:14px;">NSError *error; //加载一个NSURL对象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101120101.html"]]; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"]; NSString *text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]; NSLog(@"weatherInfo:%@", text ); </span><span style="font-size: 18px;"> </span>
1.NSDictionary类型转换为NSData类型:
//NSDictionary -> NSData: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"balance", @"key", @"remaining balance", @"label", @"45", @"value", @"USD", @"currencyCode",nil]; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:params forKey:@"Some Key Value"]; [archiver finishEncoding];
2.NSData类型转换为NSDictionary类型:
//NSData -> NSDictionary NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain]; [unarchiver finishDecoding]; [unarchiver release]; [data release];
//NSDictionary -> JSON: NSString *jsonStr=[dict JSONRepresentation];
0 0
- Newtonsoft.Json解析json数据
- 使用Json解析Json数据
- JavaScript 解析 JSON 数据
- JavaScript 解析 JSON 数据
- JAVA解析json数据
- jquery解析json数据
- jquery解析json数据
- android---Json数据解析
- Android Json 数据解析
- jquery解析json数据
- Gson解析Json数据
- json 的数据解析
- Android解析json数据
- Java解析JSON数据
- Jquery解析json数据
- eval 解析 json 数据
- 解析JSON数据
- JSON 数据解析
- druid简单教程
- C API libhdfs
- 不连续取球(取球博弈)
- ??jQuery.fn =jQuery.prototype
- Hadoop系列——MapReduce过程简版
- JSON数据解析
- 更改IP
- 【转载】用BATOOL进行Pro*C程序制造的一点心得
- CVPR2014关于DL的文章列表
- PHP基于数组的分页函数(核心函数array_slice())
- 【Linux】搜集的一些故障诊断时常用命令
- Leetcode【29】:Substring with Concatenation of All Words
- 高通研发出无线充电系统
- VLC详细的使用说明以及配置说明综合示范实例精通VLC开发