UI18-使用NSJSONSerialization方法解析JSON
2017-09-04 14:57
232 查看
使用系统自带的NSJSONSerialization解析JSON数据
定义一下属性,我们将返回的JSOB数据一层一层转化
@property (nonatomic,
copy) NSArray *results;
@property (nonatomic,
copy) NSDictionary *location;
@property (nonatomic,
copy) NSDictionary *now;
@property (nonatomic,copy)
NSDictionary *weatherdata;
-(void)fetchFeed {
NSError *error;
//加载一个NSURL对象
//???代表key,这里使用的是心知天气的api,密钥需要自己申请.
NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL
URLWithString:@"https://api.seniverse.com/v3/weather/now.json?key=???????&location=beijing&language=zh-Hans&unit=c"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection
sendSynchronousRequest:request
returningResponse:nil
error:nil];
//自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization
JSONObjectWithData:response
options:NSJSONReadingMutableLeaves
error:&error];
//将字典转化为数组
self.results = weatherDic[@"results"];
//数组中只有一个字典元素,取出这个元素转化为字典
self.weatherdata =
self.results[0];
//读取键location所对应的值,将其准换为字典
self.location =
self.weatherdata[@"location"];
//读取键now所对应的值,将其准换为字典
self.now =
self.weatherdata[@"now"];
}
JSON返回数据格式:
定义一下属性,我们将返回的JSOB数据一层一层转化
@property (nonatomic,
copy) NSArray *results;
@property (nonatomic,
copy) NSDictionary *location;
@property (nonatomic,
copy) NSDictionary *now;
@property (nonatomic,copy)
NSDictionary *weatherdata;
-(void)fetchFeed {
NSError *error;
//加载一个NSURL对象
//???代表key,这里使用的是心知天气的api,密钥需要自己申请.
NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL
URLWithString:@"https://api.seniverse.com/v3/weather/now.json?key=???????&location=beijing&language=zh-Hans&unit=c"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection
sendSynchronousRequest:request
returningResponse:nil
error:nil];
//自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization
JSONObjectWithData:response
options:NSJSONReadingMutableLeaves
error:&error];
//将字典转化为数组
self.results = weatherDic[@"results"];
//数组中只有一个字典元素,取出这个元素转化为字典
self.weatherdata =
self.results[0];
//读取键location所对应的值,将其准换为字典
self.location =
self.weatherdata[@"location"];
//读取键now所对应的值,将其准换为字典
self.now =
self.weatherdata[@"now"];
}
JSON返回数据格式:
{"results":[{"location":{"id":"WX4FBXXFKE4F","name":"北京","country":"CN","path":"北京,北京,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"多云","code":"4","temperature":"28"},"last_update":"2017-09-04T14:55:00+08:00"}]}
相关文章推荐
- 使用 Qjson 解析 JSON 数据的方法
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- 使用eval函数来解析json对象,并且使用jquery的each方法来遍历
- C++ Builder 使用 JsonCpp 解析json格式数据方法( 支持 UNICODE (UTF-16) / UTF-8 )
- Android中使用Gson解析JSON数据的两种方法
- 网络传输中,对Json组织方式的数据的解析以及使用这个方法,模拟从服务器端下载数据
- Android中的httpClient使用及JSON格式数据的解析方法
- js解析字符串为json对象使用eval(str)方法
- 使用Python解析JSON数据的基本方法
- 使用Gson解析json在打包混淆时出错的解决方法
- Goole官方网络框架-Volley的使用解析Json以及加载网络图片方法
- jQuery UI Autocomplete 使用 ajax 方法传输Json数据出现乱码问题的解决
- IOS开发学习27 ObjectC 自带json解析方法的使用
- json使用键值解析方法
- ios中json的四种解析方法的使用和比较
- Android中使用Gson解析JSON数据的两种方法
- Android中使用Gson解析JSON数据的两种方法
- Android中使用Gson解析JSON数据的两种方法
- ajax中使用JSON的方法解析
- Android网络开发中如何使用JSON进行网络通信---Android_JSON数据通讯方法解析