这是从服务器接受过来的JSON
{
HeWeather6 = (
{
basic = {
"admin_area" = beijing;
cid = CN101010100;
cnty = China;
lat = "39.90498734";
location = beijing;
lon = "116.4052887";
"parent_city" = beijing;
tz = "+8.00";
};
"daily_forecast" = (
{
"cond_code_d" = 302;
"cond_code_n" = 302;
"cond_txt_d" = Thundershower;
"cond_txt_n" = Thundershower;
date = "2018-08-13";
hum = 74;
mr = "07:14";
ms = "20:40";
pcpn = "0.0";
pop = 25;
pres = 1007;
sr = "05:25";
ss = "19:12";
"tmp_max" = 30;
"tmp_min" = 24;
"uv_index" = 5;
vis = 18;
"wind_deg" = 36;
"wind_dir" = NE;
"wind_sc" = "1-2";
"wind_spd" = 3;
},
{
"cond_code_d" = 302;
"cond_code_n" = 305;
"cond_txt_d" = Thundershower;
"cond_txt_n" = "Light Rain";
date = "2018-08-14";
hum = 70;
mr = "08:25";
ms = "21:14";
pcpn = "2.0";
pop = 64;
pres = 1008;
sr = "05:26";
ss = "19:10";
"tmp_max" = 29;
"tmp_min" = 24;
"uv_index" = 3;
vis = 17;
"wind_deg" = 54;
"wind_dir" = NE;
"wind_sc" = "1-2";
"wind_spd" = 6;
},
{
"cond_code_d" = 302;
"cond_code_n" = 305;
"cond_txt_d" = Thundershower;
"cond_txt_n" = "Light Rain";
date = "2018-08-15";
hum = 70;
mr = "09:35";
ms = "21:47";
pcpn = "0.0";
pop = 2;
pres = 1012;
sr = "05:27";
ss = "19:09";
"tmp_max" = 31;
"tmp_min" = 24;
"uv_index" = 4;
vis = 20;
"wind_deg" = 72;
"wind_dir" = NE;
"wind_sc" = "1-2";
"wind_spd" = 7;
}
);
status = ok;
update = {
loc = "2018-08-13 15:58";
utc = "2018-08-13 07:58";
};
}
);
}
如何解析
//
// ViewController.m
// 天气预报的请求
//
// Created by 开朗的男子 on 2018/8/13.
// Copyright © 2018年 开朗的男子. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//这里我把我账号key改成xxxxxxxx了,你们自己可以注册一个
NSURL *url = [NSURL URLWithString:@"https://free-api.heweather.com/s6/weather/forecast?location=CN101010100&key=xxxxxxxxxx&lang=en"];
//创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 最简单的错误处理机制:
if (data && !error) {
// JSON格式转换成字典,IOS5中自带解析类NSJSONSerialization从response中解析出数据放到字典中
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
//NSLog(@"%@", obj);
//NSLog(@"%@", [[obj objectForKey:@"HeWeather6"][0] objectForKey:@"basic"]);
NSLog(@"%@", obj[@"HeWeather6"][0][@"daily_forecast"][0][@"cond_txt_d"]);
}
}];
[dataTask resume];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
输出结果
2018-08-13 16:13:06.615544+0800 天气预报的请求[5659:258492] Thundershower
这样就单独解析出来了一个数据