【问题标题】:JSON Parsing with NSDictionary and finding value by key使用 NSDictionary 解析 JSON 并按键查找值
【发布时间】:2013-12-17 02:16:13
【问题描述】:

我有一个简单的NSDictionary,我试图通过返回的 JSON 填充来自外部站点的数据。返回的 JSON 很好,但我无法获取特定键的实际数据。

这是打印到控制台的 JSON 数据。

这是我的 JSON 数据:

(
        {
        CategoryID = 12345;
        CategoryName = "Baked Goods";
    },
        {
        CategoryID = 12346;
        CategoryName = Beverages;
    },
        {
        CategoryID = 12347;
        CategoryName = "Dried Goods";
    },
        {
        CategoryID = 12348;
        CategoryName = "Frozen Fruit & Vegetables";
    },
        {
        CategoryID = 12349;
        CategoryName = Fruit;
    },
        {
        CategoryID = 12340;
        CategoryName = "Purees & Soups";
    },
        {
        CategoryID = 12341;
        CategoryName = Salad;
    },
        {
        CategoryID = 12342;
        CategoryName = "Snack Items";
    },
        {
        CategoryID = 12343;
        CategoryName = Vegetables;
    }
)

我得到的错误是:

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[__NSCFArray enumerateKeysAndObjectsUsingBlock:]: 无法识别的选择器发送到 实例 0x6884000'

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    NSError *error = nil;
    // Get the JSON data from the website
    NSDictionary *categories = [NSJSONSerialization JSONObjectWithData:data  options:kNilOptions error:&error];

    if (categories.count > 0){
        NSLog(@"This is my JSON data %@", categories);

        [categories enumerateKeysAndObjectsUsingBlock: ^(__strong id key, __strong id obj, BOOL *stop) {
        NSLog(@"Key = %@, Object For Key = %@", key, obj); }];
}

我不确定为什么会发生这种情况,但我确定这很简单,就像我使用了不正确的对象之类的东西。

感谢您的帮助。

【问题讨论】:

  • 尝试从错误中解决。它告诉你你需要的一切。

标签: ios json nsdictionary


【解决方案1】:

+JSONObjectWithData:options:error: 返回的是 NSArray 而不是 NSDictionary。 '-[__NSCFArray enumerateKeysAndObjectsUsingBlock:] 是错误消息的关键部分。它告诉你你正在一个数组上调用-enumerateKeysAndObjectsUsingBlock:


对于这种情况,您可以改用-enumerateObjectsUsingBlock:

如果不确定返回的是 NSArray 还是 NSDictionary,可以使用-isKindOf:

id result = [NSJSONSerialization …];
if ([result isKindOf:[NSArray class]]) {
    NSArray *categories = result;
    // Process the array
} else if ([result isKindOf:[NSDictionary class]]) {
    NSDictionary *categories = result;
    // Process the dictionary
}

来自enumerateObjectsUsingBlock:

使用数组中的每个对象执行给定的块,从第一个对象开始,一直到数组中的最后一个对象。

  • (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

所以应该这样称呼

[categories enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"index = %d, Object For Key = %@", idx, obj);
}];

快速阅读文档确实可以为您节省很多挫败感。

【讨论】:

  • 谢谢,我以前看过 isKindOfClass,这肯定有帮助。我将代码放在 NSArray 中,现在我得到一个不同的错误......
  • 谢谢,我以前看过 isKindOfClass,这肯定有帮助。我将代码放在 NSArray 中,现在我得到了一个不同的错误...不兼容的块指针类型将 'void(^)(__strong id, __strong id, BOOL *)' 发送到 'void(^)( __strong id, NSUInteger, BOOL *)' 我知道第二个参数有问题,但它期望什么无符号整数值?我只是想为所有键及其关联值迭代数组。
  • 谢谢,我是 iOS 新手。我发现文档有点像黑洞。一旦我明白为什么有些东西不起作用,我就会看到文档的使用......无论如何,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
相关资源
最近更新 更多