2015-05-06 278 views
1

我想正确定位Json输出内的元素,并且我越来越近,但我认为这是一种简单而明显的方式。正确解析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"]给我的关键值,但我需要这些键的关联值。

+0

它看起来不像聊天是一个字典数组。它看起来像一个字典阵列数组。 – gnasher729

回答

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"]); 
+0

我想我看到你正在尝试做什么,但它会产生一个'__NSArrayM objectForKeyedSubscript:]:无法识别的选择器发送到实例'error – memyselfandmyiphone

+0

你能显示原始的JSON吗? – jjv360

+0

干杯。已更新上面 – memyselfandmyiphone