【问题标题】:Parsing a Django-created JSON解析 Django 创建的 JSON
【发布时间】:2013-01-27 08:16:51
【问题描述】:

我认识到已经有很多关于此的 SO 主题,但它们似乎都已经过时了。

IE:SBJSON parsing issue with Twitter's GET trends/:woeidJSONValue ARC issue

但是,我的 JSON 响应有点不同。

这是原始字符串(从 Django 后端创建):

 [
  {"user":  "[
              {\"id\": \"48\"}, 
              {\"email_address\": null}, 
              {\"password\": \"f41fd61838bc65d6b2c656d488e33aba\"}, 
              {\"salt\": \"24\"}, 
              {\"date_created\": \"2013-01-27 07:59:26.722311+00:00\"},
              {\"date_modified\": \"2013-01-27 07:59:26.722357+00:00\"}, 
              {\"is_deleted\": \"False\"}
            ]"
   }
 ]

阻止我只使用 SBJson 的事情是 SBJSonParser 和/或 Apple NSJSONSeriliazatoin 类 + 方法是 "user": 之后和第二个 [ 之前的两个引号(以及它的封闭引号表亲,在倒数第二个])。

在将NSMutableString 转换为 JSON 对象时,这些引号会混淆上述两种解决方案。

在删除有问题的引号和/或有效处理它们的 JSON 解析库方面有什么建议/解决方案?

NSScanner 和一些 NSMutableString 类方法,但没有什么特别明显的想法出现在我的脑海中。

寻找一个简单的新颖解决方案。

【问题讨论】:

  • 最简单的解决方案是确保您的后端生成有效的 JSON,但事实并非如此。
  • 下一个最简单的解决方案是将出现的“[ 和 ]”替换为 [ 和 ]。请参阅下面的答案。

标签: ios xcode sbjson nsjsonserialization


【解决方案1】:

正如其他人所说,这不是有效的 JSON。在后端修复它是最好的解决方案,但如果你不能 - 这应该适合你:

NSString *dJangoString = @"[{\"user\":  \"[{\"id\": \"48\"},{\"email_address\": null},{\"password\":\"f41fd61838bc65d6b2c656d488e33ab\"},{\"salt\": \"24\"},{\"date_created\": \"2013-01-27 07:59:26.722311+00:00\"},{\"date_modified\": \"2013-01-27 07:59:26.722357+00:00\"},{\"is_deleted\": \"False\"}]\"}]\"";

dJangoString = [dJangoString stringByReplacingOccurrencesOfString:@"\"[" withString:@"["];
dJangoString = [dJangoString stringByReplacingOccurrencesOfString:@"]\"" withString:@"]"];

NSData* dJangoData = [dJangoString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
id retObj = [NSJSONSerialization JSONObjectWithData:dJangoData options:0 error:&error];

NSLog(@"retObj = %@", [retObj description]);

【讨论】:

  • 谢谢,是的,我和我们的后端人员讨论过。我也有点困惑,以前从未见过像这样格式化的东西。无论如何,在讨论之后,我现在开始使用格式良好(正确读取)的 JSON! :( 浪费了 3 个小时。
【解决方案2】:

您显示的 JSON 有点.. 无效... 上面是嵌套的 JSON:

  1. 带有用户键的 dict 数组 == 另一个 JSON 的字符串
  2. 一个包含 n 个 JSON 字典的数组

我猜它可以分两步解析......

    id s = @"[ \n \
            {\"user\": \"[%@]\" \n \
            } \n \
     ]";
    id s2 = @"{\\\"id\\\": \\\"48\\\"},{\\\"email_address\\\": \\\"null\\\"},{\\\"password\\\": \\\"f41fd61838bc65d6b2c656d488e33aba\\\"},{\\\"salt\\\": \\\"24\\\"},{\\\"date_created\\\": \\\"2013-01-27 07:59:26.722311+00:00\\\"},{\\\"date_modified\\\": \\\"2013-01-27 07:59:26.722357+00:00\\\"},{\\\"is_deleted\\\": \\\"False\\\"}";
    s = [NSString stringWithFormat:s,s2];
    id d = [s dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n%@ %@", s, d);
    NSError *error = nil;
    id outerJSON = [NSJSONSerialization JSONObjectWithData:d options:0 error:&error];
    if(!outerJSON && error) {
        NSLog(@"%@", error);
        return -1;
    }
    NSLog(@"\n%@", outerJSON);

    s = [[outerJSON objectAtIndex:0] objectForKey:@"user"];
    d = [s dataUsingEncoding:NSUTF8StringEncoding];
    id innerJSON = [NSJSONSerialization JSONObjectWithData:d options:0 error:&error];
    if(!innerJSON && error) {
        NSLog(@"%@", error);
        return -1;
    }
    NSLog(@"\n%@", innerJSON);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2021-02-02
    相关资源
    最近更新 更多