我一直在努力尝试在 IOS5 中按顺序解析我的 JSON。它是从服务器按顺序发送的,所以我知道不是这样的。这是我的代码:
NSArray *userData = [update JSONValue];
NSLog(@"USERDATA%@", userData);
NSEnumerator *enumerator = [userData objectEnumerator];
id key;
while (key = [enumerator nextObject]) {
NSDictionary *value = key;
NSString *comment = [value objectForKey:@"comment"];
NSLog(@"USERCOMMENT %@", comment);
}
第一个NSLog,一切看起来都很漂亮。第二个 NSLog 给我的一切都乱了。我已经无计可施了。
第一个 NSLOG:
USERDATA{
1 = {
comment = "Test 6";
photoID = 1;
postedDate = "2 days ago";
userID = 17;
userPic = "members/0/image01.png";
username = kismet;
};
2 = {
comment = "Test 5";
photoID = 1;
postedDate = "2 days ago";
userID = 17;
userPic = "members/0/image01.png";
username = kismet;
};
3 = {
comment = "Test 4";
photoID = 1;
postedDate = "2 days ago";
userID = 17;
userPic = "members/0/image01.png";
username = kismet;
};
4 = {
comment = "Test 3";
photoID = 1;
postedDate = "2 days ago";
userID = 17;
userPic = "members/0/image01.png";
username = kismet;
};
5 = {
comment = "Test 2";
photoID = 1;
postedDate = "2 days ago";
userID = 17;
userPic = "members/0/image01.png";
username = kismet;
};
6 = {
comment = "Test 1";
photoID = 1;
postedDate = "2 days ago";
userID = 17;
userPic = "members/0/image01.png";
username = kismet;
};
}
第二个 NSLog:
USERCOMMENT Test 4
USERCOMMENT Test 6
USERCOMMENT Test 1
USERCOMMENT Test 3
USERCOMMENT Test 5
USERCOMMENT Test 2
最佳答案
问题是你的顶级对象不是 NSArray,而是 NSDictionary。如果您发回一个数组,它将正常工作。另一种方法是获取顶级字典的键并在迭代之前对它们进行排序。
关于ios - 在IOS中按顺序解析JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11377264/