AFNetWorking-1-获得数据

AFNetWorking是比较常用的网络解析的库,今天研究一下返回的Json格式以及其中内容的解析。下载地址:https://github.com/AFNetworking/AFNetworking/

里面也有Example,不过确实看不懂写的什么。所以自己尝试用自己的方式来解释一下这个东东。

首先新建一个项目,将AFNetworking,添加至项目中。在pch添加预编译头文件。


StoryBoard中一个页面+一个按钮就可以。我这个例子,主要是看返回的Json数据都有哪些东东,以及提取数据。

TestVC.h代码:

#import <UIKit/UIKit.h>

@interface TestVC : UIViewController

- (IBAction)jsonTap:(id)sender;

@end


.m文件代码:

#import "TestVC.h"

static NSString *const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";    //声明一个静态变量,这个静态变量就是下json的地址

@interface TestVC ()

@property(strong,nonatomic)NSDictionary *wDic;    //存储天气的json

@property(strong,nonatomic)NSArray *wKey;     //存储天气的key

@end


- (IBAction)jsonTap:(id)sender

{

    NSString *weatherURL=

    [NSString stringWithFormat:@"%@weather.php?format=json",BaseURLString];

    //得到json的URL

    NSURL *url=[NSURL URLWithString:weatherURL];   //将字符串转为URL

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    //新建一个针对URL的请求

    AFJSONRequestOperation *operation=[AFJSONRequestOperation JSONRequestOperationWithRequest:request

        success:^(NSURLRequest *request,NSHTTPURLResponse *response,id json){

            self.wDic=(NSDictionary *)json;   //将json转换为字典并存储

       }

        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

            UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"未下载" message: [NSString stringWithFormat:@"%@",error]delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [av show];

        }];

    [operation start];

    //-------打印Json数据的代码-----------

    self.wKey=[self.wDic allKeys];

    for (NSString *key in self.wKey)

    {

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

    }

    //-------打印Json数据的代码-----------

}


打印出的Json数据为:

2013-06-07 09:58:04.460 AFK_Study[779:c07] 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;

        }

    );

    request =     (

                {

            query = "Lat 32.35 and Lon 141.43";

            type = LatLon;

        }

    );

    weather =     (

                {

            date = "2013-01-15";

            precipMM = "1.8";

            tempMaxC = 12;

            tempMaxF = 53;

            tempMinC = 10;

            tempMinF = 50;

            weatherCode = 119;

            weatherDesc =             (

                                {

                    value = Cloudy;

                }

            );

            weatherIconUrl =             (

                                {

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

                }

            );

            winddir16Point = NNW;

            winddirDegree = 348;

            winddirection = NNW;

            windspeedKmph = 66;

            windspeedMiles = 41;

        },

                {

            date = "2013-01-16";

            precipMM = "0.6";

            tempMaxC = 13;

            tempMaxF = 56;

            tempMinC = 11;

            tempMinF = 51;

            weatherCode = 113;

            weatherDesc =             (

                                {

                    value = Sunny;

                }

            );

            weatherIconUrl =             (

                                {

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

                }

            );

            winddir16Point = WNW;

            winddirDegree = 284;

            winddirection = WNW;

            windspeedKmph = 33;

            windspeedMiles = 21;

        },

                {

            date = "2013-01-17";

            precipMM = "0.5";

            tempMaxC = 14;

            tempMaxF = 56;

            tempMinC = 7;

            tempMinF = 44;

            weatherCode = 119;

            weatherDesc =             (

                                {

                    value = Cloudy;

                }

            );

            weatherIconUrl =             (

                                {

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

                }

            );

            winddir16Point = WNW;

            winddirDegree = 293;

            winddirection = WNW;

            windspeedKmph = 41;

            windspeedMiles = 25;

        },

                {

            date = "2013-01-18";

            precipMM = "1.9";

            tempMaxC = 11;

            tempMaxF = 51;

            tempMinC = 7;

            tempMinF = 44;

            weatherCode = 353;

            weatherDesc =             (

                                {

                    value = "Light rain shower";

                }

            );

            weatherIconUrl =             (

                                {

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

                }

            );

            winddir16Point = NW;

            winddirDegree = 312;

            winddirection = NW;

            windspeedKmph = 66;

            windspeedMiles = 41;

        },

                {

            date = "2013-01-19";

            precipMM = "1.1";

            tempMaxC = 7;

            tempMaxF = 45;

            tempMinC = 6;

            tempMinF = 43;

            weatherCode = 176;

            weatherDesc =             (

                                {

                    value = "Patchy rain nearby";

                }

            );

            weatherIconUrl =             (

                                {

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

                }

            );

            winddir16Point = NW;

            winddirDegree = 324;

            winddirection = NW;

            windspeedKmph = 52;

            windspeedMiles = 32;

        }

    );

}


OK,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、付费专栏及课程。

余额充值