json 解析



JSON数据结构以其轻量化的结构体和良好的可读性被越来越广泛的运用,特别在移动开发上,手机的流量是宝贵资源,更要求使用轻量级的数据格式进行数据传输。关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如SBJson、JSONFramwork等,用的也非常广泛,自从iOS5.0以后,苹果推出了SDK自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也是比其他第三方开源项目的高很多,详情可查看Developer Guider


关于NSJSONSerialization,官方文档中有如下介绍:

You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.

An object that may be converted to JSON must have the following properties:

  • The top level object is an NSArray or NSDictionary.

  • All objects are instances of NSStringNSNumberNSArrayNSDictionary, or NSNull.

  • All dictionary keys are instances of NSString.

  • Numbers are not NaN or infinity.

我们能利用NSJSONSerialization将JSON转换成Foundation对象,也能将Foundation对象转换成JSON,转换成JSON的对象必须具有如下属性:

  • 顶层对象必须是NSArray或者NSDictionary
  • 所有的对象必须是NSString、NSNumber、NSArray、NSDictionary、NSNull的实例
  • 所有NSDictionary的key必须是NSString类型
  • 数字对象不能是非数值或无穷
接下来看看如何使用,首先是如何生成JSON格式的数据:
我这里选用项目中的代码片段来进行简要介绍,以下显示了登陆请求JSON格式数据的生成

NSDictionary中的key就是json字符串中的key,object就是json字符串中的value,isValidJSONObject:方法是检测Foundation对象能否合法转换为JSON对象,dataWithJSONObject:options:error 方法是将Foundation对象转换为JSON对象,参数NSJSONWritingPrettyPrinted的意思是将生成的json数据格式化输出,这样可读性高,不设置则输出的json字符串就是一整行。

    NSError *error;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSDictionary *weatherInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:
                                    &error];
    NSDictionary *weatherInfo = [weatherInfoDic objectForKey:@"weatherinfo"];
    
    self.weather.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n",[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]];



以上代码打印结果如下:

2013-12-11 14:38:52.799 jsonTest[604:a0b] Register json:{
  "_id" : "test",
  "word" : "passWord",
  "loginName" : "username"
}


下面是如何将json格式的数据转化为foundation对象类型。


    NSError *error;
   
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"
http://m.weather.com.cn/data/101010100.html"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   
    NSDictionary *weatherInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:
                                    &error];
    NSDictionary *weatherInfo = [weatherInfoDic objectForKey:@"weatherinfo"];
   
    self.weather.text = [NSString stringWithFormat:@"
%@\n%@\n%@\n%@\n%@\n",[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"date_y"],

    [weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]];

   

    NSLog(@"weatherInfo 字典里面的内容为-->%@",weatherInfoDic);


    结果打印为:

     2013-12-11 14:38:52.797 jsonTest[604:a0b] weatherInfo 字典里面的内容为--> {
    weatherinfo =     {
        city = "\U5317\U4eac";
        "city_en" = beijing;
        cityid = 101010100;
        date = "";
        "date_y" = "2013\U5e7412\U670811\U65e5";
        fchh = 11;
        fl1 = "\U5c0f\U4e8e3\U7ea7\U8f6c3-4\U7ea7";
        fl2 = "3-4\U7ea7";
        fl3 = "\U5c0f\U4e8e3\U7ea7";
        fl4 = "\U5c0f\U4e8e3\U7ea7";
        fl5 = "\U5c0f\U4e8e3\U7ea7";
        fl6 = "\U5c0f\U4e8e3\U7ea7";
        fx1 = "\U5fae\U98ce";
        fx2 = "\U5317\U98ce";
        img1 = 0;
        img10 = 1;
        img11 = 1;
        img12 = 99;
        img2 = 99;
        img3 = 0;
        img4 = 99;
        img5 = 0;
        img6 = 99;
        img7 = 0;
        img8 = 99;
        img9 = 0;
        "img_single" = 0;
        "img_title1" = "\U6674";
        "img_title10" = "\U591a\U4e91";
        "img_title11" = "\U591a\U4e91";
        "img_title12" = "\U591a\U4e91";
        "img_title2" = "\U6674";
        "img_title3" = "\U6674";
        "img_title4" = "\U6674";
        "img_title5" = "\U6674";
        "img_title6" = "\U6674";
        "img_title7" = "\U6674";
        "img_title8" = "\U6674";
        "img_title9" = "\U6674";
        "img_title_single" = "\U6674";
        index = "\U8f83\U51b7";
        index48 = "\U51b7";
        "index48_d" = "\U5929\U6c14\U51b7\Uff0c\U5efa\U8bae\U7740\U68c9\U670d\U3001\U7fbd\U7ed2\U670d\U3001\U76ae\U5939\U514b\U52a0\U7f8a\U6bdb\U886b\U7b49\U51ac\U5b63\U670d\U88c5\U3002\U5e74\U8001\U4f53\U5f31\U8005\U5b9c\U7740\U539a\U68c9\U8863\U3001\U51ac\U5927\U8863\U6216\U539a\U7fbd\U7ed2\U670d\U3002";
        "index48_uv" = "\U5f31";
        "index_ag" = "\U6781\U4e0d\U6613\U53d1";
        "index_cl" = "\U9002\U5b9c";
        "index_co" = "\U8f83\U8212\U9002";
        "index_d" = "\U5efa\U8bae\U7740\U539a\U5916\U5957\U52a0\U6bdb\U8863\U7b49\U670d\U88c5\U3002\U5e74\U8001\U4f53\U5f31\U8005\U5b9c\U7740\U5927\U8863\U3001\U5462\U5916\U5957\U52a0\U7f8a\U6bdb\U886b\U3002";
        "index_ls" = "\U57fa\U672c\U9002\U5b9c";
        "index_tr" = "\U9002\U5b9c";
        "index_uv" = "\U4e2d\U7b49";
        "index_xc" = "\U9002\U5b9c";
        st1 = 5;
        st2 = "-4";
        st3 = 2;
        st4 = "-5";
        st5 = 4;
        st6 = "-4";
        temp1 = "6\U2103~-3\U2103";
        temp2 = "3\U2103~-6\U2103";
        temp3 = "4\U2103~-6\U2103";
        temp4 = "5\U2103~-4\U2103";
        temp5 = "5\U2103~-4\U2103";
        temp6 = "5\U2103~-3\U2103";
        tempF1 = "42.8\U2109~26.6\U2109";
        tempF2 = "37.4\U2109~21.2\U2109";
        tempF3 = "39.2\U2109~21.2\U2109";
        tempF4 = "41\U2109~24.8\U2109";
        tempF5 = "41\U2109~24.8\U2109";
        tempF6 = "41\U2109~26.6\U2109";
        weather1 = "\U6674";
        weather2 = "\U6674";
        weather3 = "\U6674";
        weather4 = "\U6674";
        weather5 = "\U6674\U8f6c\U591a\U4e91";
        weather6 = "\U591a\U4e91";
        week = "\U661f\U671f\U4e09";
        wind1 = "\U5fae\U98ce\U8f6c\U5317\U98ce3-4\U7ea7";
        wind2 = "\U5317\U98ce3-4\U7ea7";
        wind3 = "\U5fae\U98ce";
        wind4 = "\U5fae\U98ce";
        wind5 = "\U5fae\U98ce";
        wind6 = "\U5fae\U98ce";
    };
}

   


NSJSONReadingMutableLeaves 所在枚举中包含的值为:

  NSJSONReadingMutableContainers 
  NSJSONReadingMutableLeaves 
  NSJSONReadingAllowFragments



以下 为  JSONObjectWithData: options: error: 方法的注释。

/* Create a Foundation object from JSON data.

Set theNSJSONReadingAllowFragments option if the parser shouldallow top-level objects that are not an NSArray or NSDictionary.Setting theNSJSONReadingMutableContainers option will make the parsergenerate mutable NSArrays and NSDictionaries

Setting theNSJSONReadingMutableLeaves option will make the parsergenerate mutable NSString objects.

If an error occurs during the parse, then the error parameter will be set and the result will be nil.
  The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
 */


从以上①、② 、③点的说明中,我们应该能很清楚的知道该参数的作用。





   


   










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值