`
wenxin2009
  • 浏览: 314438 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ios中json解析

    博客分类:
  • ios
阅读更多

以下是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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics