0

Here is a response I get from a server:

{
    result = {
        blankfill = "[[1, [u'32354'], False, 7]]";
        choice = "[[13, [3], False, 11], [10, [0], False, 3], [9, [1], False, 3], [2, [2], False, 4], [3, [1], False, 3], [1, [2], False, 3]]";
        spendTime = 26;
    };
    retcode = 0;
    subjectid = 1;
    submittime = "2016-05-24T15:21:50.784"; 
}

choice is the string I want to convert to NSArray, it also contains nested arrays.

Here is the code I tried:

 NSString *jsonString = [responseObject[@"result"] objectForKey:@"choice"];
 NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
 NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 

Currently, arr is always nil.

4
  • 1
    "You can see that, choice is a NSString actually stands for a NSArray also containing nested array" It's just a string containing some commas and brackets. Commented May 24, 2016 at 15:42
  • 1
    You always get nil, because that's not a JSON, JSON does not have False as value, see json.org. However, it would have false. u'32354' is also not a value. Commented May 24, 2016 at 15:46
  • @simpleBob: The False is just five characters within a string. The key "choice" has a string value "[[13, [3], False..." Commented May 24, 2016 at 16:32
  • @gnasher729 yes, it's just five characters in a string, but when JSONObjectWithData:options:error: is called, it should gets transformed, but it can't be transformed because it is not a valid JSON, since False doesn't exist in JSON Commented May 24, 2016 at 16:38

1 Answer 1

1

You will need to change False to false everywhere in your string. Then it will parse as JSON.

(It seems likely that something is wrong at the server end, though. Presumably this is supposed to be a JSON array to start with, and because of this capitalization mistake, it is arriving as a string instead.)

NSString* s = @"[[13, [3], false, 11], [10, [0], false, 3], [9, [1], false, 3], [2, [2], false, 4], [3, [1], false, 3], [1, [2], false, 3]]";
NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
NSError* err;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
NSLog(@"%@", arr);

Logs:

(
        (
        13,
                (
            3
        ),
        0,
        11
    ),
        (
        10,
                (
            0
        ),
        0,
        3
    ),
        (
        9,
                (
            1
        ),
        0,
        3
    ),
        (
        2,
                (
            2
        ),
        0,
        4
    ),
        (
        3,
                (
            1
        ),
        0,
        3
    ),
        (
        1,
                (
            2
        ),
        0,
        3
    )
)
Sign up to request clarification or add additional context in comments.

4 Comments

It works. Sorry I didn't notice the False. I get the response from Python restful server. True/False is valid in python, so I think they just return String format for their Boolean field. Sorry for the noise. I will accept the answer.
The real problem with your code was error:nil. The error was explaining to you what the problem was, but you were throwing away the error without reading it.
See Introducing JSON, only true, false and null are valid literal values, not True and False. What Python accepts is beside the point. As @matt points out, error message are your friend, use them.
Yes,you are right. I will pay more attention to those error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.