带有“("")”的JSON括号不等于“[”] "]" [英] JSON with "(" ")" brackets istead of "[" "]"

查看:152
本文介绍了带有“("")”的JSON括号不等于“[”] "]"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

in .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);

之后我在Console(currentCondition)中得到了这个:

After this I got this in 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;
    }
)

我无法解决这个问题。

And I can t work with this.

如果我在safari中打开此网址:

If I open this url in safari:

current_condition:[{cloudcover:0 ,......所以有[不是(

"current_condition": [ {"cloudcover": "0", ..... So there is "[" not "("

所以我的json错了,在xcode中

So my json is wrong, in xcode

我该怎么办?

推荐答案

您在日志输出中看到的只是集合的格式 NSArray NSDictionary 对象,而不是实际的JSON本身。哪里有 [和日志语句中的,你有一个数组;其中有一个 {你有一本字典。

What you are seeing in your log output is just the formatting of a collection of NSArray and NSDictionary objects, not the actual JSON itself. Where there's a [ in the source JSON and a ( in your log statements, you have an array; where there's a { you have a dictionary.

所以你需要从单元素数组中提取字典才能使用它:

So you need to extract the dictionary from the single-element array before you can use it:

NSDictionary* currentCondition = fullDictFromjson[@"current_condition"][0];

这篇关于带有“("")”的JSON括号不等于“[”] "]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆