json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子。

而不格式化的时候json和xml 又是一个普普通通的字符串,在网络通信的时候也只需要请求一次,而不用每次为得到木一个值而重复的请求服务器或者目标主机,

json和xml 都采用 键 - 值 的形式来存放数据。

xml 使用: <键> 值 </键>

json 使用:  "键" : "值"

苹果公司提供了一个官方的json解析库 NSJSONSerialization

NSJSONSerialization  里面包含了两个方法来通过不同数据形式来解析json数据。

1、+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; //使用缓冲区数据来解析

2、+ (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error; // 使用文件流的形式来解析json

/* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate 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.
*/
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; /* Create a JSON object from JSON data stream. The stream should be opened and configured. All other behavior of this method is the same as the JSONObjectWithData:options:error: method.
*/
+ (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error;

解析json的步骤大概是,先把json字符串读取到缓冲区,然后使用NSJSONSerialization    里面的方法进行解析,根据不同json 格式可能返回的数据类型不一样,所以最好用 id 类型接。

eg:  id dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];

在得到解析后的数据时,然后就一步一步 ,一个一个 键 - 值 的去寻找你想要的咚咚。

Eg 1 : 从本地文件中读取json数据,然后读取到缓冲区。

- (void)jsonParse{

    //初始化文件路径。
NSString* path = [[NSBundle mainBundle] pathForResource:@"nanjing" ofType:@"txt"];
//将文件内容读取到字符串中,注意编码NSUTF8StringEncoding 防止乱码,
NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//将字符串写到缓冲区。
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//解析json数据,使用系统方法 JSONObjectWithData: options: error:
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
//接下来一步一步解析。知道得到你想要的东西。
NSArray* arrayResult =[dic objectForKey:@"results"];
NSDictionary* resultDic = [arrayResult objectAtIndex:];
NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];
NSLog(@"geometryDic: %@, resultDic:%@",geometryDic,resultDic);
NSDictionary* locationDic = [geometryDic objectForKey:@"location"];
NSNumber* lat = [locationDic objectForKey:@"lat"];
NSNumber* lng = [locationDic objectForKey:@"lng"];
NSLog(@"lat = %@, lng = %@",lat,lng);
[jsonString release]; }

Eg 2 :使用网络路径来解析json,

- (void)jsonParse{

    //初始化网络路径。
NSString* path = @"http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";
//初始化 url
NSURL* url = [NSURL URLWithString:path];
//将文件内容读取到字符串中,注意编码NSUTF8StringEncoding 防止乱码,
NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
//将字符串写到缓冲区。
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//解析json数据,使用系统方法 JSONObjectWithData: options: error:
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]; //一下为自定义解析, 自己想怎么干就怎么干 NSArray* arrayResult =[dic objectForKey:@"results"];
NSDictionary* resultDic = [arrayResult objectAtIndex:];
NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];
NSLog(@"geometryDic: %@, resultDic:%@",geometryDic,resultDic);
NSDictionary* locationDic = [geometryDic objectForKey:@"location"];
NSNumber* lat = [locationDic objectForKey:@"lat"];
NSNumber* lng = [locationDic objectForKey:@"lng"];
NSLog(@"lat = %@, lng = %@",lat,lng);
[jsonString release]; }

Eg 3 :使用网络路径来解析json 。 使用NSURLRequest 和NSURLConnection 请求网络数据。

- (void)jsonParse{

    //初始化网络路径。
NSString* path = @"http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";
//初始化 url
NSURL* url = [NSURL URLWithString:path];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
//将请求到的字符串写到缓冲区。
NSData* jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//解析json数据,使用系统方法 JSONObjectWithData: options: error:
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]; //一下为自定义解析, 自己想怎么干就怎么干 NSArray* arrayResult =[dic objectForKey:@"results"];
NSDictionary* resultDic = [arrayResult objectAtIndex:];
NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];
NSLog(@"geometryDic: %@, resultDic:%@",geometryDic,resultDic);
NSDictionary* locationDic = [geometryDic objectForKey:@"location"];
NSNumber* lat = [locationDic objectForKey:@"lat"];
NSNumber* lng = [locationDic objectForKey:@"lng"];
NSLog(@"lat = %@, lng = %@",lat,lng); }

demo下载 :http://download.csdn.net/download/wsq724439564/6207829

最新文章

  1. auto(c++11)
  2. (译)iOS Code Signing: 解惑
  3. apache开源项目--Mavibot
  4. php 计算一个字符串在另一个字符串中出现的次数
  5. BZOJ1533: [POI2005]Lot-A Journey to Mars
  6. FWA winner | Car Visualizer WebGL
  7. Eclipse Package Explorer视图无法打开
  8. HDU 5037 FROG (贪婪)
  9. Hibernate之深入Hibernate的映射文件
  10. 汇编实验一 查看CPU和内存,用机器指令和汇编语言指令编程
  11. Fiddler的配置
  12. 使用Java+MySQL+Apache开发后台项目(一)
  13. fullpage 中输入框弹起 页面上移问题处理
  14. js控制easyui文本框例子及控制html例子
  15. centos7忘记登录密码修改
  16. html 基础之canvas 和 localStorage
  17. Category Theory: 01 One Structured Family of Structures
  18. DataAnnotations 验证
  19. [整理]C语言中字符常量与ASCII码
  20. python安装openSSL

热门文章

  1. 基于Node.js的实时推送 juggernaut
  2. 了解RFC协议号
  3. 第二百九十六天 how can I 坚持
  4. linux 下终端复用软件推荐——tmux
  5. 给 TTreeView 添加复选框
  6. BAT-使用BAT方法清理系统垃圾
  7. Spring EL hello world example
  8. BestCoder Round #68 (div.2) geometry(hdu 5605)
  9. jeecms支持的freemaker标签大全
  10. 如何将std::string转int,double? (C/C++) (C) (template)