سؤال

I recognize that there are a lot of SO topics on this already but all of them seem quite dated.

IE: SBJSON parsing issue with Twitter's GET trends/:woeid and JSONValue ARC issue

However, my JSON response is a little different.

This is the raw string (created from a Django backend):

 [
  {"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\"}
            ]"
   }
 ]

The thing stopping me from just using SBJson and it's SBJSonParser and/or the Apple NSJSONSeriliazatoin class + methods are the two quotation marks after "user": and before the second [ (as well as it's enclosing quote cousin, after the second last ]).

Those quotes mess up both of the mentioned solutions in converting NSMutableString into a JSON object.

Any advice/solutions in either removing the offending quotes and/or a JSON parsing library that deals with them effectively?

NSScanner and perhaps some NSMutableString class methods but nothing exceptionally obvious springs to my mind.

Looking for a simple novel solution.

هل كانت مفيدة؟

المحلول

Like the others have said, that is not valid JSON. Fixing it on the backend would be the best solution, but if you cannot - this should work for you:

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

نصائح أخرى

the JSON you show is kinda.. not valid... The above is a nested JSON:

  1. an array with dict with a user key == String of another JSON
  2. an array with n dicts of JSON

it could be parsed in 2 steps I guess...

    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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top