JSON解析---初识

JSONJavaScript Object Notation

     是一种轻量级的数据交换格式

     完全独立于语言的文本格式

     易于人阅读和编写

     易于解析和生成 (网络传输速度快)


JSON语法规则

     数据在 名称/ 对中

     数据有逗号分隔

     花括号保存对象

     方括号保存数组




以下是一个.JSON 文件


{

    "class1":[

          {

            "name":"zhangsan",

            "age":"14",

            "tel":"123123123"

          },

         

         {

           "name":"lisi",

           "age":"13",

           "tel":"123123123"

         },

         

         {

           "name":"wangwu",

           "age":"12",

           "tel":"123123123"

         }


    ],

    "class2":[

              {

              "name":"zhaoliu",

              "age":"15",

              "tel":"123123123"

              },

              

              {

              "name":"wuwu",

              "age":"16",

              "tel":"123123123"

              },

              

              {

              "name":"liuliu",

              "age":"17",

              "tel":"123123123"

              } 

              ]

}


  ViewController.m文件

//  JSON解析


#import "ViewController.h"

#import "JSONKit.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    NSString * path= [[NSBundle mainBundle] pathForResource:@"test.json" ofType:nil];

    

    NSData * data=[NSData dataWithContentsOfFile:path];

    

//    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    /*

    //NSLog(@"%@",dic);    //打印整个JSON文件

     */

    

    /*

    NSArray * array=dic[@"class1"];   //拆一层

    NSLog(@"%@",array);

    */

   

    /*

    NSArray * array=dic[@"class1"];

    for(NSDictionary * dic in array)

    {

        NSLog(@"%@",dic);     //拆两层

    }

     */

    

    //得到model

    /*

    NSArray * array=dic[@"class1"];   //拆一层

    //NSLog(@"%@",array);

    for( NSDictionary * dic in array)

    {

        NSData * data=[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];

        NSString * str=[[NSString  alloc]initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"%@",str);

    }

    */

    

    //打印第一组的人员姓名

    /*

    NSArray * array=dic[@"class1"];   //拆一层

    //NSLog(@"%@",array);

    NSMutableArray * marray=[[NSMutableArray alloc]init];

    for( NSDictionary * dic in array)

    {

        [marray addObject:dic[@"name"]];

    }

    NSLog(@"%@",marray);

    */


//引入JSONKit

    //打印整个JSON文件

    /*

    NSDictionary * dic=[data objectFromJSONData];

    NSLog(@"%@",dic);

    */


    /*

    NSString * str=@"{\"name\":\"zhangsan\"}";

    NSData * datas=[str dataUsingEncoding:NSUTF8StringEncoding];

    id obj=[datas objectFromJSONData];

    NSLog(@"%@",obj);

    */


}


@end


PS:推荐一个网站 www.bejson.com 可以将没有组织结构的.JSON 文件上传,就可以轻松的转变成可视化的视图结构。





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值