【问题标题】:Parsing Json Output correctly正确解析 Json 输出
【发布时间】:2015-05-06 15:51:34
【问题描述】:

我正在尝试正确定位 Json 输出中的元素,并且我越来越接近,但我认为我缺少一种简单而明显的方法。

我的 Json 看起来像这样,带有上层事件。

JSON 片段已更新

chat =     (
            (
                    {
            Key = senderId;
            Value =                 {
                Type = 0;
                Value = "eu-west-1:91afbc3f-890a-4160-8903-688bf0e9efe8";
            };
        },
                    {
            Key = chatId;
            Value =                 {
                Type = 0;
                Value = "eu-west-1:be6457ce-bac1-412d-9307-e375e52e22ff";
            };
        },
                    {
            Key = timestamp;
            Value =                 {
                Type = 1;
                Value = 1430431197;
            };
        },

//Continued

我的目标是使用

NSArray *chat = array[@"chat"];

for ( NSDictionary *theCourse in chat )
{
    NSLog(@"---- %@", theCourse);

    // I tried the following to target the values 
    //NSLog(@"chatId: %@", [theCourse valueForKey:@"Key"]);
    //NSLog(@"timestamp: %@", theCourse[@"senderId"] ); 
} 
}

我需要解析每个键的值数据,如果我使用数组会像 [theCourse valueForKey:@"Key"] 那样做,但我认为我可能不够深入?

如您所料,[theCourse valueForKey:@"Key"] 给了我键值,但我需要这些键的关联值。

【问题讨论】:

  • chat 看起来不像是一个字典数组。它看起来像一个字典数组。

标签: ios objective-c json nsdictionary


【解决方案1】:

您可以创建更简单的字典:

NSArray *chat = array[@"chat"][0];
NSMutableDictionary* newDict = [NSMutableDictionary dictionary];
for (NSDictionary* d in chat)
    [newDict setValue:d[@"Value"][@"Value"] forKey:d[@"Key"]];

现在您可以使用newDict

NSLog(@"chatId: %@", [newDict valueForKey:@"chatId"]);

【讨论】:

  • 我想我看到了你正在尝试做的事情,但它会产生 __NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 错误
  • 干杯。以上更新
  • 那行得通。只给我 1 次迭代,而不是 JSON 中的每个 chatId,但我认为这是另一个问题!谢谢
猜你喜欢
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多