gpt4 book ai didi

ios - IOS中按顺序解析JSON

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:07 25 4
gpt4 key购买 nike

我一直在努力让我的 JSON 在 IOS5 中按顺序解析。它是按顺序从服务器传来的,所以我知道不是这样。这是我的代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com