iOS中,如何解析一个JSON天气预报

这是从服务器接受过来的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

这样就单独解析出来了一个数据

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值