inc*_*iko 1 cocoa-touch json ios
在.m中:
@implementation ViewController
{
NSDictionary *_json;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL* url = [NSURL URLWithString:@"http://api.worldweatheronline.com/free/v1/weather.ashx?q=Si%C3%B3fok&format=json&num_of_days=5&key=mykey"];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error;
_json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary* fullDictFromjson = _json[@"data"];
NSLog(@"%@",fullDictFromjson);
NSDictionary* currentCondition = fullDictFromjson[@"current_condition"];
NSLog(@"%@",currentCondition);
Run Code Online (Sandbox Code Playgroud)
之后,我在Console(currentCondition)中得到了这个:
2013-04-18 22:18:16.758 weather[18111:c07] (
{
cloudcover = 0;
humidity = 74;
"observation_time" = "08:18 PM";
precipMM = "0.0";
pressure = 1019;
"temp_C" = 11;
"temp_F" = 51;
visibility = 10;
weatherCode = 113;
weatherDesc = (
{
value = Clear;
}
);
weatherIconUrl = (
{
value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png";
}
);
winddir16Point = S;
winddirDegree = 170;
windspeedKmph = 5;
windspeedMiles = 3;
}
)
Run Code Online (Sandbox Code Playgroud)
我不能处理这个。
如果我在野生动物园中打开此网址:
“ current_condition”:[{“ cloudcover”:“ 0”,.....所以有“ [”而不是“(”
所以我的json是错误的,在xcode中
我该怎么办?
您在日志输出中看到的只是NSArrayand NSDictionary对象集合的格式,而不是实际的JSON本身。在[源JSON和(日志语句中都有的地方,您有一个数组。那里有{一本字典。
因此,您需要先从单元素数组中提取字典,然后才能使用它:
NSDictionary* currentCondition = fullDictFromjson[@"current_condition"][0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3639 次 |
| 最近记录: |