IOS学习之——JSON解析

概述:

Jason是一种流行的编码格式。编码很简单,一共只有两种:1 { } 表示的字典类型  2 [] 包括的数组类型。

json编码分析:

虽然Jason的语法简单到爆,可是它表达的内容并不简单,通过两种语法的相互嵌套,你实现无比复杂的数据结构。很有点道家“道生一,一生二,二生三,三生万物。万物负阴而抱阳,冲气以为和。”的味道。


Json文档结构:

  1. 对象
    1. {“name":"a三哥”,”age":17}
  2. 数组
    1. [“text”,”html”,”css”]
  3. 在线解析
    1. http://json.tongxiehui.net/

IOS JSON官方文档

有需要的朋友可以参考一下

NSJSONSerialization(JSON序列化)

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

你可以使用NSJSONSerialization 类把json和foundation对象相互转化。

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.

一个将要被转化为json的对象必须符合如下要求:
    顶层的对象是NSArray或者NSDictionary类型的。
      所以的对象是的NSStringNSNumberNSArrayNSDictionary类型的实例,或者是NSNull
        所有字典的key必须是NSString类型的。
          数字不能是NaN或者无穷大。

          Other rules may apply. Calling isValidJSONObject: or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.

          其他的条件可能被应用,调用 isValidJSONOBject:或者尝试转换是最后的方法来分辨所给对象是否可以转换成JSON类型的数据。

          Inheritance 继承


          Conforms To  符合


          Import Statement  包含声明


          OBJECTIVE-C

          @import Foundation;

          Availability 可用


          Available in iOS 5.0 and later.

          Creating a JSON Object (创建一个JSON对象)

          简单的解析代码

          这里用到了阿里的一个JSON接口,代码:https://git.oschina.net/zhengaoxing/IOS_JSON
          //
          //  ViewController.m
          //  Jason
          //
          //  Created by Bc_Ltf on 15/3/26.
          //  Copyright (c) 2015年 Bc_ltf. All rights reserved.
          //
          
          #import "ViewController.h"
          
          @interface ViewController ()
          
          @end
          
          @implementation ViewController
          
          - (void)viewDidLoad {
              [super viewDidLoad];
              [self setup];
          
          }
          
          - (void)didReceiveMemoryWarning {
              [super didReceiveMemoryWarning];
              // Dispose of any resources that can be recreated.
          }
          
          -(void)setup
          {
              NSError *error;
              //加载一个NSURL对象
              NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://gc.ditu.aliyun.com/geocoding?a=%E8%8B%8F%E5%B7%9E%E5%B8%82"]];
              //将请求的url数据放到NSData对象中  <span style="font-size:14px;">本文原创,转载请注明出处:</span><span style="font-size:14px;">http://blog.csdn.net/zhenggaoxing/article/details/44649827</span>
              NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
              //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
              NSDictionary *ali = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
              //    NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
          
              NSLog(@"现在你可以看到Jason内容了:%@", ali );
          }
          
          @end
          

          本文原创,转载请注明出处: http://blog.csdn.net/zhenggaoxing/article/details/44649827

          • 2
            点赞
          • 0
            收藏
            觉得还不错? 一键收藏
          • 0
            评论
          评论
          添加红包

          请填写红包祝福语或标题

          红包个数最小为10个

          红包金额最低5元

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

          抵扣说明:

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

          余额充值