iPhone - 访问NSDictionary

时间:2010-01-06 12:16:40

标签: iphone json nsdictionary

我有一个JSON字符串,我将其转换为NSDictionary,并且我将关键字“people”的值获取为NSDictionary:

NSDictionary *testDict = [jsonString JSONValue];
NSDictionary *peopleDict = [testDict objectForKey:@"people"];

一个NSLog of peopleDict

NSLog(@"%@", peopleDict);

返回以下内容:

{
0 =     {
    id = 1;
    name = Doe;
    date = "Fri Dec 04 13:50:30 +0200 2009";
};
1 =     {
    id = 20;
    name = Mr. T;
    date = "Fri Nov 18 17:55:30 +0200 2009";
};
2 =     {
    id = 100;
    name = TestName;
    date = "Mon Nov 30 12:00:10 +0200 2009";
};
}

结果的数量,我知道(在这种情况下为3)。 但是我如何访问行0..2和键值? id是一个int值,name为String,date也是String。

有人知道吗?

提前多多感谢&最诚挚的问候。

1 个答案:

答案 0 :(得分:7)

for (id key in peopleDict) {
  NSLog(@"key: %@, value: %@", key, [peopleDict objectForKey:key]);
}

这是link to NSDictionary reference

您可能需要查看:访问键和值方法:

  • allKeys
  • allKeysForObject:
  • allValues
  • getObjects:andKeys:
  • objectForKey:
  • objectsForKeys:notFoundMarker:
  • valueForKey: