六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 486|回复: 0

ios中json解析

[复制链接]
 楼主| 发表于 2013-9-9 22:00:46 | 显示全部楼层 |阅读模式
以下是ios中三种不同解析方式:

jsonkit需要导入JSONKit.h、JSONKit.m文件,可在官网上下https://github.com/johnezang/JSONKit
  1、JSONKit解析方式:
  1. NSString *jsonString = @"[{"age":18,"book":{"price":23.2,"title":"booooooook1"},"name":"samyou"},{"age":22,"book":{"price":21,"title":"booooooook2"},"name":"samsam"}]";

  2.     NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  3. //    NSDictionary *resultDic = [jsonData objectFromJSONData];
  4.     NSArray *resultDic = [jsonData objectFromJSONData];
  5.     NSString *nstr = [resultDic JSONString];//json字符串
  6.     NSLog(@"str : %@",nstr);
  7.     NSLog(@"age= %@", [resultDic valueForKey:@"age"]);
  8.     NSLog(@"book= %@", [resultDic valueForKey:@"book"]);
  9.     NSArray *books = [resultDic valueForKey:@"book"];
  10.     NSLog(@"book.price===%@",[books valueForKey:@"price"]);
复制代码
2、SBJSON解析方式(通过plist文件进行读取):
https://github.com/stig/json-framework可下载。

data.plist文件内容:[{"auctionId":1000,"auctionName":"苹果"},{"auctionId":1001,"auctionName":"李子"}]
  1. //json解析
  2.     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
  3.     //获取字典
  4.     NSDictionary *dataDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
  5.     //获取json key值
  6.     NSString *jsonData = [dataDict objectForKey:@"auction"];
  7.     if(jsonData == nil){
  8.         NSLog(@"无数据!");
  9.     }else{
  10.         //得到jsoin数组
  11.         NSArray *jsonArray = [jsonData JSONValue];
  12.         NSLog(@"jsonArray:%@",jsonArray);
  13.         //通过key获取对应的值
  14.         //写法一
  15.         NSString *auctionId = [jsonArray valueForKey:AUCTION_ID];
  16.         NSLog(@"auctionId : %@",auctionId);
  17.         //写法二
  18. //        NSArray *auctionId = [jsonArray valueForKey:AUCTION_ID];
  19. //        for(int i=0;i<[auctionId count];i++){
  20. //            NSLog(@"auction : %@",[auctionId objectAtIndex:i]);
  21. //        }
  22.         //写法三(以下方法不行)
  23. //        for(int i=0;i<[jsonArray count];i++){
  24. //            Auction *auction = [jsonArray objectAtIndex:i];
  25. //            NSLog(@"Auction.auctionName : %@",auction.auctionName);
  26. //        }
  27.   
  28.     }
  29.     [jsonData release];
复制代码
3、ios5自带API进行json解析,NSJSONSerialization类
  1. NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
  2.         [dictionary setValue:@"Anthony" forKey:@"name"];
  3.         [dictionary setValue:[NSNumber numberWithUnsignedInteger:30] forKey:@"Age"];
  4.         NSArray *arrayChildren = [[NSArray alloc] initWithObjects:@"A", @"B",  nil];
  5.         [dictionary setValue:arrayChildren forKey:@"children"];
  6.         NSError *error = nil;
  7.         NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
  8.         NSLog(@"jsonData : %@",[jsonData description]);
  9.         if (error) {
  10.             NSLog(@"dic->%@",error);
  11.         }
  12.         id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
  13.         if (nil != jsonObject) {
  14.             if ([jsonObject isKindOfClass:[NSDictionary class]]){
  15.                 NSDictionary *resultDic = (NSDictionary *)jsonObject;
  16.                 NSLog(@"Received JSON Dictionary : %@", resultDic);
  17.             } else {
  18.                 NSLog(@"Error JSON data.");
  19.             }
  20.         }
复制代码
本文摘自:http://wenxin2009.iteye.com/blog/1671691



该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表