【问题标题】:NSJSONSerialization only getting root keyNSJSONSerialization 只获取根密钥
【发布时间】:2012-08-23 15:31:59
【问题描述】:

我在使用 NSJSONSerialization 从 PHP 服务器解析 JSON 时遇到问题。 JSLint 说我的 JSON 是有效的,但似乎只能进入一两个级别。

这本质上是我的 JSON 结构:

{
    "products":
    [{
        "product-name":
        {
            "product-sets":
            [{
                "set-3":
                {
                    "test1":"test2",
                    "test3":"test4"
                },
                "set-4":
                {
                    "test5":"test6",
                    "test7":"test8"
                }
            }]
        },
        "product-name-2":
        {
            "product-sets":
            [{

            }]
        }
    }]
}

这是我的解析代码:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if (json) {
    NSArray *products = [json objectForKey:@"products"];              // works
    for (NSDictionary *pItem in products) {                           // works
        NSLog(@"Product: %@", pItem);                                 // works, prints the entire structure under "product-name"
        NSArray *productSets = [pItem objectForKey:@"product-sets"];  // gets nil
        for (NSDictionary *psItem in productSets) {
            // never happens
        }
    }
}

我已经为此旋转了几个小时,但在我搜索的任何地方都找不到类似的东西。是否有任何我不知道的限制,或者我只是没有看到明显的东西?

【问题讨论】:

  • 对我来说产品集似乎是空的。没有项目,没有迭代
  • 这是有道理的,但是我在产品集中添加了一些值,但我仍然遇到同样的问题。我将编辑问题以包含此内容。谢谢!

标签: objective-c ios json parsing nsjsonserialization


【解决方案1】:

你错过了一个嵌套对象

NSArray *productSets = [[pItem objectForKey:@"product-name"] objectForKey:@"product-sets"];

我用这个 CLI 程序测试了它

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {


        NSString *jsonString = @"{\"products\":[{\"product-name\": {\"product-sets\": {\"set-3\":{\"test1\":\"test2\", \"test3\":\"test4\"}, \"set-4\":{\"test5\":\"test6\", \"test7\":\"test8\"} }}}, {\"product-name-2\": \"2\"}]}";
        // insert code here...
        NSLog(@"%@", jsonString);
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];

        if (json) {
            NSArray *products = [json objectForKey:@"products"];              // works
            for (NSDictionary *pItem in products) {                           // works
                NSLog(@"Product: %@", pItem);                                 // works, prints the entire structure under "product-name"
                NSArray *productSets = [[pItem objectForKey:@"product-name"] objectForKey:@"product-sets"];  // gets nil
                for (NSDictionary *psItem in productSets) {
                    NSLog(@"%@", psItem);
                }
            }
        }

    }
    return 0;
}

请注意,您的 json 中的某些内容很奇怪:

对于每个展平的对象,键应该相同。包含数字或对象的键没有多大意义。如果您需要跟踪单个对象,请包含具有适当值的 id 键。

【讨论】:

  • 就是这样,谢谢!我发现关于 NSJSONSerialization 使用的文档非常有限,而且我没有意识到我需要嵌套的 objectForKey 调用。另外,感谢您对 JSON 的评论,我对 iOS 和正确的 JSON 都比较陌生。
  • 这实际上与 NSJSONSerialization 无关。它提供 NSArray 和 NSDictionaries,你必须熟悉这些类。
  • 这就解释了为什么我找不到任何有用的解决方案,非常感谢
猜你喜欢
  • 2020-07-19
  • 2019-01-26
  • 2017-05-09
  • 2016-08-19
  • 2018-03-02
  • 2018-11-29
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多