@JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成
@JSON 语法规则:
1.JSON 语法是 JavaScript 对象表示语法的子集。
数据在名称/值对中
数据由逗号分隔
花括号保存对象
方括号保存数组
JSON 名称/值对
2.JSON 数据的书写格式是:名称/值对。
名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:"firstName":"John"
3.JSON 值可以是:
数字(整数或浮点数)
字符串(在双引号中)
逻辑值(true 或 false)
数组(在方括号中)
对象(在花括号中)
null
@JSON建构有两种结构:
json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构
1、对象:对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。
2、数组:数组在js中是中括号“[]”括起来的内容,数据结构为 ["java","javascript","vb",...],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。
经过对象、数组2种结构就可以组合成复杂的数据结构了。
@基础示例
---整体是个对象,对象的key是"programmers",value是一个数组,数组里面又包含3个对象
{"programmers":[
{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
{"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
{"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
],
"authors":[
{"firstName":"Isaac","lastName":"Asimov","genre":"sciencefiction"},
{"firstName":"Tad","lastName":"Williams","genre":"fantasy"},
{"firstName":"Frank","lastName":"Peretti","genre":"christianfiction"}
],
"musicians":[
{"firstName":"Eric","lastName":"Clapton","instrument":"guitar"},
{"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"}
]}
@具体形式:
1
2
3
4
|
{
"姓名":"大憨",
"年龄":24
}
|
1
2
3
4
5
6
7
|
{
"学生":
[
{"姓名":"小明","年龄":23},
{"姓名":"大憨","年龄":24}
]
}
|
@系统提供的JSON解析类----NSJSONSerialization
#pragma mark - 系统提供的JSON解析类 - (void)onClickSystem{ // 加载一个本地的JSON文件路径,放到NSData对象中去 NSData * jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"txt"]]; NSError * error = nil; // 把一个json格式的data,解析为OC中的数据对象(一般为数组或字典) id obj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; // 判断一下,id是NSMutableArray类型还是NSMutableDictionary if ([obj isKindOfClass:[NSArray class]]) { NSMutableArray * array = obj; NSLog(@"*************%@",array); }else{ NSMutableDictionary * dic = obj; NSLog(@"???????????????%@",dic); } // 把一个OC中的数据对象(数组或对象--在OC中类似字典)转换成JSON格式的数据 NSDictionary * dic = @{@"to": @"壮壮",@"content":@"晚上见",@"from": @"晓宇",@"data": @"2014.04.18"}; NSLog(@"DIC = %@",dic); NSData * data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error]; // 把一个json格式的data,解析为OC中的NSString类型对象 // 无特殊意义,一般用来查看JSON文本封装成 数组 还是 对象(OC中为字典) NSString * jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"-----------------------------%@",jsonString); }
@第三方类----JSONKit
#pragma mark - 第三方类JSONKit - (void)onClickJSONKit{ #pragma mark 把JSON格式的数据转换成OC中的数据对象 // 从文档中读取json格式的Data NSData * jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"txt"]]; // json格式的Data转化为OC对象 id obj1 = [jsonData objectFromJSONData]; NSLog(@"%@",obj1); // 从文档中读取json格式的String NSString * jsonString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]; // json格式的String转化为OC对象 id obj2 = [jsonString objectFromJSONString]; NSLog(@"%@",obj2); #pragma mark 把一个OC中的数据对象转换成JSON格式的数据 NSDictionary * dic = @{@"to": @"壮壮",@"content":@"晚上见",@"from": @"晓宇",@"data": @"2014.04.18"}; // 转化为json格式的Data NSData * dataJson = [dic JSONData]; // 转化为json格式的String NSString * stringJson = [dic JSONString]; }
@代码样例:
{ "country": { "province": { "-number": "3", "-name": "河北", "city": [ { "-number": "9", "-name": "张家口市", "area": [ { "-number": "146", "#text": "桥西区" }, { "-number": "147", "#text": "桥东区" }, { "-number": "148", "#text": "宣化区" }, { "-number": "149", "#text": "下花园区" }, { "-number": "150", "#text": "宣化县" }, { "-number": "151", "#text": "张北县" }, { "-number": "152", "#text": "康保县" }, { "-number": "153", "#text": "沽源县" }, { "-number": "154", "#text": "尚义县" }, { "-number": "162", "#text": "崇礼县" } ] }, { "-number": "10", "-name": "承德市", "area": [ { "-number": "163", "#text": "双桥区" }, { "-number": "164", "#text": "双滦区" }, { "-number": "165", "#text": "鹰手营子矿区" }, { "-number": "166", "#text": "承德县" }, { "-number": "167", "#text": "兴隆县" }, { "-number": "168", "#text": "平平县" }, { "-number": "169", "#text": "滦平县" } ] }, { "-number": "11", "-name": "沧州市", "area": [ { "-number": "174", "#text": "运河区" }, { "-number": "175", "#text": "新华区" }, { "-number": "176", "#text": "泊头区" }, { "-number": "177", "#text": "任丘区" }, { "-number": "185", "#text": "肃宁县" }, { "-number": "186", "#text": "南皮县" } ] }, { "-number": "12", "-name": "廊坊市", "area": [ { "-number": "190", "#text": "安次区" }, { "-number": "191", "#text": "广阳区" }, { "-number": "192", "#text": "霸州市" }, { "-number": "193", "#text": "三河市" }, { "-number": "194", "#text": "固安县" }, { "-number": "195", "#text": "永清县" } ] }, { "-number": "13", "-name": "衡水市", "area": [ { "-number": "202", "#text": "桃城区" }, { "-number": "203", "#text": "冀州市" }, { "-number": "204", "#text": "深州市" }, { "-number": "205", "#text": "枣强县" }, { "-number": "206", "#text": "武邑县" } ] } ] } } }
#pragma mark - 系统类NSJSONSerialization - (void)initializeAllDataOfNSJSONSerialization{ self.allProvinceArray = [NSMutableArray array]; NSData * jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"hebei" ofType:@"txt"]]; NSError * error = nil; id obj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; NSMutableDictionary * countryDic = obj; NSDictionary * countryInfo = [countryDic objectForKey:@"country"]; NSDictionary * provinceInfo = [countryInfo objectForKey:@"province"]; NSString * pName = [provinceInfo objectForKey:@"-name"]; NSString * pNumber = [provinceInfo objectForKey:@"-number"]; HMTProvince * province = [[HMTProvince alloc]initWithName:pName withNumber:pNumber]; [self.allProvinceArray addObject:province]; NSArray * cityArray = [provinceInfo objectForKey:@"city"]; for (int i = 0; i < [cityArray count]; i++) { NSDictionary * cityInfo = [cityArray objectAtIndex:i]; NSString * cName = [cityInfo objectForKey:@"-name"]; NSString * cNumber = [cityInfo objectForKey:@"-number"]; HMTCity * city = [[HMTCity alloc]initWithName:cName withNumber:cNumber]; [province.cityArray addObject:city]; NSArray * areaArray = [cityInfo objectForKey:@"area"]; for (int j = 0; j < [areaArray count]; j++) { NSString * aName = [[areaArray objectAtIndex:j] objectForKey:@"#text"]; NSString * aNumber = [[areaArray objectAtIndex:j] objectForKey:@"-number"]; HMTArea * area = [[HMTArea alloc]initWithName:aName withNumber:aNumber]; [city.areaArray addObject:area]; [area release]; } [city release]; } [province release]; } #pragma mark - 系统类JSONKit - (void)initializeAllDataOfJSONKit{ self.allProvinceArray = [NSMutableArray array]; NSData * jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"hebei" ofType:@"txt"]]; id obj = [jsonData objectFromJSONData]; NSMutableDictionary * countryDic = obj; NSDictionary * countryInfo = [countryDic objectForKey:@"country"]; NSDictionary * provinceInfo = [countryInfo objectForKey:@"province"]; NSString * pName = [provinceInfo objectForKey:@"-name"]; NSString * pNumber = [provinceInfo objectForKey:@"-number"]; HMTProvince * province = [[HMTProvince alloc]initWithName:pName withNumber:pNumber]; [self.allProvinceArray addObject:province]; NSArray * cityArray = [provinceInfo objectForKey:@"city"]; for (int i = 0; i < [cityArray count]; i++) { NSDictionary * cityInfo = [cityArray objectAtIndex:i]; NSString * cName = [cityInfo objectForKey:@"-name"]; NSString * cNumber = [cityInfo objectForKey:@"-number"]; HMTCity * city = [[HMTCity alloc]initWithName:cName withNumber:cNumber]; [province.cityArray addObject:city]; NSArray * areaArray = [cityInfo objectForKey:@"area"]; for (int j = 0; j < [areaArray count]; j++) { NSString * aName = [[areaArray objectAtIndex:j] objectForKey:@"#text"]; NSString * aNumber = [[areaArray objectAtIndex:j] objectForKey:@"-number"]; HMTArea * area = [[HMTArea alloc]initWithName:aName withNumber:aNumber]; [city.areaArray addObject:area]; [area release]; } [city release]; } [province release]; }