AFNetworking解析Json-2-分类解释

AFK解析Json的使用-2-

AFK解析例子中,自带了两个分类(方法列表):NSDictionary+weather_package和NSDictionary+weather。这两个分类的方法就是专门解析出相应数据的。我们再来看一下Json的数据结构:

key为:data,值为:{

    "current_condition" =     (

                {

            cloudcover = 16;

            humidity = 59;

            "observation_time" = "09:09 PM";

            precipMM = "0.1";

            pressure = 1010;

            "temp_C" = 10;

            "temp_F" = 49;

            visibility = 10;

            weatherCode = 113;

            weatherDesc =             (

                                {

                    value = Clear;

                }

            );

            weatherIconUrl =             (

                                {

                    value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png";

                }

            );

            winddir16Point = NW;

            winddirDegree = 316;

            windspeedKmph = 47;

            windspeedMiles = 29;

        }

    ); //---此处之前是key为 current_condition的部分


    request =     (

                {

            query = "Lat 32.35 and Lon 141.43";

            type = LatLon;

        }

    );

//---这是第2部分--key为 request


    weather =     (  //这里以后是第3部分--key为 weather

                {

            date = "2013-01-15";

            precipMM = "1.8";

            tempMaxC = 12;

             //---后面省略------

           

而 NSDictionary+weather_package.m的方法列表就是解析出这三个部分并转化为相应的数据结构:

-(NSDictionary *)currentCondition{

    NSDictionary *dict = [self objectForKey:@"data"];

    NSArray *ar = [dict objectForKey:@"current_condition"];

    return [ar objectAtIndex:0];

}


-(NSDictionary *)request{

    NSDictionary *dict = [self objectForKey:@"data"];

    NSArray *ar = [dict objectForKey:@"request"];

    return [ar objectAtIndex:0];

}


-(NSArray *)upcomingWeather{

    NSDictionary *dict = [self objectForKey:@"data"];

    return [dict objectForKey:@"weather"];

}

而其中数据比较重要的就是key为weather部分。所以又加了一个NSDictionary+weather的分类,专门解析里面的内容。例如其中一个方法是这么写的:

-(NSString *)weatherIconURL{

    NSArray *ar = [self objectForKey:@"weatherIconUrl"];

    NSDictionary *dict = [ar objectAtIndex:0];

    return [dict objectForKey:@"value"];

}

解析出来的值就是weatherIconUrl =             (

                                {

                    value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png";

                }

部分的png地址。

为了看字典内容,我自己添加了一个方法:

-(void)printDic    //打印字典的key和值

{

    for (NSString *key in self)

     {

         NSLog(@"key为:%@,值为:%@",key,[self objectForKey:key]);

     }

}

接下来通过一个具体的实例,来构建解析Json的应用--得到数据。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

limaning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值