解析Django创建的JSON

时间:2013-01-27 08:16:51

标签: ios xcode sbjson nsjsonserialization

我认识到已经有很多关于这方面的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类方法,但没有什么特别明显的东西出现在我脑海中。

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

2 个答案:

答案 0 :(得分:1)

你展示的JSON有点......无效... 以上是嵌套的JSON:

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

    我可以分两步解析...

        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);
    

答案 1 :(得分: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]);