IOS JSON解析
概述:
json编码分析:
Json文档结构:
- 对象
-
- {“name":"a三哥”,”age":17}
- 数组
-
- [“text”,”html”,”css”]
- 在线解析
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
orNSDictionary
. -
All objects are instances of
NSString
,NSNumber
,NSArray
,NSDictionary
, orNSNull
. -
All dictionary keys are instances of
NSString
. -
Numbers are not NaN or infinity.
NSString
, NSNumber
, NSArray
, NSDictionary类型的
实例,或者是NSNull
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.
简单的解析代码
// // 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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。