【问题标题】:Parsing JSON in order in IOS在IOS中按顺序解析JSON
【发布时间】:2012-07-07 17:41:46
【问题描述】:

我一直在尝试在 IOS5 中 IN ORDER 解析我的 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

【问题讨论】:

  • 你能发布一个 JSON 样本和生成的日志吗?

标签: ios json ios5


【解决方案1】:

问题是您的顶级对象不是 NSArray,而是 NSDictionary。如果你发回一个数组,那将正常工作。另一种方法是获取顶级字典的键并在迭代之前对其进行排序。

【讨论】:

    【解决方案2】:

    字典的条目是无序的。当您遍历字典时,您可以按任意顺序获取键。

    【讨论】:

    • 但是从我的代码来看,我的推理是NSDictionary的值应该只获取NSArray键的一个对象,所以理论上顺序应该是NSArray userData的顺序。
    【解决方案3】:

    除非您编写自己的 JSON 解析器,否则您无法做到这一点。任何自尊的 JSON 库都不能保证你的顺序,如果它想符合 JSON 规范。

    从JSON对象的定义:

    键值对的无序集合。

    【讨论】:

    • 您的元素可能会按顺序排列或出现故障,我的意思是不能保证任何特定的顺序。有时你可能会很幸运并把它们整理好,但作为一名工程师,你不应该指望这一点。如果您自己解析响应,则可以按原始顺序返回项目。
    【解决方案4】:

    这可能与您无关,但我总是在服务器上进行排序并返回一个有序的 JSON 字符串。

    【讨论】:

      【解决方案5】:

      对于那些仍然对解决方案感兴趣的人:

      NSDictionary *userData = [update JSONValue];
      NSArray *keys = [[userData allKeys] sortedArrayUsingSelector:@selector(compare:)];
      NSMutableArray *array = [NSMutableArray arrayWithCapacity: [keys count]];
      
      int i = 0;
      for (NSString *key in keys) {
          [array addObject: [userData objectForKey: key]];
          i++;
      }
      
      for (NSDictionary *myDict in array) {
          NSString *comment = [myDict objectForKey:@"comment"];
          NSLog(@"USERCOMMENT %@", comment);
      }
      

      按顺序返回 JSON:

      USERCOMMENT Test 6
      USERCOMMENT Test 5
      USERCOMMENT Test 4
      USERCOMMENT Test 3
      USERCOMMENT Test 2
      USERCOMMENT Test 1
      

      【讨论】:

        猜你喜欢
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 2012-06-23
        • 2013-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多