以下是ios中三种不同解析方式:
jsonkit需要导入JSONKit.h、JSONKit.m文件,可在官网上下https://github.com/johnezang/JSONKit。
1、JSONKit解析方式:
NSString *jsonString = @"[{\"age\":18,\"book\":{\"price\":23.2,\"title\":\"booooooook1\"},\"name\":\"samyou\"},{\"age\":22,\"book\":{\"price\":21,\"title\":\"booooooook2\"},\"name\":\"samsam\"}]"; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; // NSDictionary *resultDic = [jsonData objectFromJSONData]; NSArray *resultDic = [jsonData objectFromJSONData]; NSString *nstr = [resultDic JSONString];//json字符串 NSLog(@"str : %@",nstr); NSLog(@"age= %@", [resultDic valueForKey:@"age"]); NSLog(@"book= %@", [resultDic valueForKey:@"book"]); NSArray *books = [resultDic valueForKey:@"book"]; NSLog(@"book.price===%@",[books valueForKey:@"price"]);
2、SBJSON解析方式(通过plist文件进行读取):
https://github.com/stig/json-framework可下载。
data.plist文件内容:[{"auctionId":1000,"auctionName":"苹果"},{"auctionId":1001,"auctionName":"李子"}]
//json解析 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //获取字典 NSDictionary *dataDict = [NSDictionary dictionaryWithContentsOfFile:filePath]; //获取json key值 NSString *jsonData = [dataDict objectForKey:@"auction"]; if(jsonData == nil){ NSLog(@"无数据!"); }else{ //得到jsoin数组 NSArray *jsonArray = [jsonData JSONValue]; NSLog(@"jsonArray:%@",jsonArray); //通过key获取对应的值 //写法一 NSString *auctionId = [jsonArray valueForKey:AUCTION_ID]; NSLog(@"auctionId : %@",auctionId); //写法二 // NSArray *auctionId = [jsonArray valueForKey:AUCTION_ID]; // for(int i=0;i<[auctionId count];i++){ // NSLog(@"auction : %@",[auctionId objectAtIndex:i]); // } //写法三(以下方法不行) // for(int i=0;i<[jsonArray count];i++){ // Auction *auction = [jsonArray objectAtIndex:i]; // NSLog(@"Auction.auctionName : %@",auction.auctionName); // } } [jsonData release];
3、ios5自带API进行json解析,NSJSONSerialization类
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; [dictionary setValue:@"Anthony" forKey:@"name"]; [dictionary setValue:[NSNumber numberWithUnsignedInteger:30] forKey:@"Age"]; NSArray *arrayChildren = [[NSArray alloc] initWithObjects:@"A", @"B", nil]; [dictionary setValue:arrayChildren forKey:@"children"]; NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error]; NSLog(@"jsonData : %@",[jsonData description]); if (error) { NSLog(@"dic->%@",error); } id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error]; if (nil != jsonObject) { if ([jsonObject isKindOfClass:[NSDictionary class]]){ NSDictionary *resultDic = (NSDictionary *)jsonObject; NSLog(@"Received JSON Dictionary : %@", resultDic); } else { NSLog(@"Error JSON data."); } }
s
相关推荐
iOS下解析json数据,自己亲自使用过,挺好用的。 使用JSONKit的解析方法:(需导入包:#import "JSONKit/JSONKit.h") - (void)printJson{ //如果json是“单层”的,即value都是字符串、数字,可以使用...
在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式,通过本文给大家介绍iOS中json解析出现的null,nil,NSNumber的解决办法,感兴趣的朋友参考下
1.简单的ios-json解析 2.在程序里的json串,解析为3个数组 3.涉及字典
本文将介绍TouchJson、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法,解析国家气象局API。通过本文给大家介绍IOS中Json解析的四种方法,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧
移动开发抓包工具和IOS JSON解析工具,希望对大家有用
iOS xml解析和json解析demo
ios下json数据解析的TouchJson的库, 欢迎下载
本demo 是解析json数据 利用 苹果自带的解析技术解析json 也是json 解析数据 推荐的和优先考虑的
JSONKIT 为IOS解析最快的JSON解析工具类.
iOS JSON XML 解析实例 e,详情见:http://blog.csdn.net/vnanyesheshou/article/details/52484367 http://blog.csdn.net/vnanyesheshou/article/details/52505430
本人一直用的ios中json解析库,简单易用!用需要的朋友可以下来
完美支持UNITY中JSON的解析和生成。IOS和Android 完全支持,比SimpleJSON更强大。c#版本
iOS5新增了JSON解析的API,我们将其和其他五个开源的JSON解析库进行了解析速度的测试,下面是测试的结果
ios下json数据解析的JsonKit库
ios下json数据解析的SBJson 库
简单的使用了json解析、AFNetworking和MJExtension
ios下json数据解析的实例,完整代码,欢迎下载 如有疑问,请参考http://blog.csdn.net/abc13939746593/article/details/8764160
NULL 博文链接:https://ytwhw.iteye.com/blog/1749858