iOS-JSON数据解格式

json数据解析

json的概念

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。
在实际开发中经常使用JSON来获取服务器上的数据,并通过解析json数据获取我们想要的数据

iOS开发使用 NSJSONSerialization(序列化)类解析json数据
NSJSONSerialization提供了Json数据封包、Json数据解析

NSJSONSerialization将JSON数据转换为NSDictionary或NSArray
解包方法,将NSDictionary、NSArray对象转换为JSON数据(可以通过调用isValidJSONObject来判断NSDictionary、NSArray对象是否可以转换为JSON数 据)封包

json数据封包

   NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil];

      // isValidJSONObject判断对象是否可以构建成json对象
      if ([NSJSONSerialization isValidJSONObject:dic]){
          NSError *error;

          // 创造一个json从Data, NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性。
          NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];

          NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
          NSLog(@"json data:%@",json);
      }

json数据解析

  NSError *error;
  //加载一个NSURL对象
  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101120101.html"]];

  //将请求的url数据放到NSData对象中
  NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];  
  
//iOS自带解析类NSJSONSerialization从response中解析出数据放到字典中
  NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

  NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];

  NSString *text = [NSString stringWithFormat:@"今天是 %@  %@  %@  的天气状况是:%@  %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];    NSLog(@"weatherInfo:%@", text );

json解析过程示例

  NSError *error;
//加载一个NSURL对象
NSURLRequest\*request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]];

//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//iOS自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];

txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];

NSLog(@"weatherInfo字典里面的内容为---%@", weatherDic );
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我们大家平时在开发 App 的时候,相信接触最多的就是 JSON 数据了。只要你的 App 有读取网络数据的功能,...
    SwiftCafe阅读 1,613评论 2 18
  • JSON JSON和XML都是需要解析的 JSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据...
    JonesCxy阅读 1,893评论 2 10
  • 1.服务器返回的数据: 对于服务器返回来的data,一般有两种格式:JSON/XMLJSON:是民间的;XML:是...
    SoftKnife阅读 568评论 0 5
  • JSON的简单介绍 1.什么是JSON(1)JSON是一种轻量级的数据格式,一般用于数据交互(2)服务器返回给客户...
    Mario_ZJ阅读 696评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139