1

This is the json coming from the server:

{
    "name":"channelname",   
    "args":
    [
        {           
            "username":"myusername",
            "message":"mymessage"
        }
    ]
} 

Using ios5 built-in json methods I try to parse out the args's username/message.

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [packet.data dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: nil];
NSDictionary *argsValues = [[NSDictionary alloc] initWithDictionary:[JSON objectForKey:@"args"]];

Second line throws an error:

dictionary argument is not an NSDictionary

When I NSlog [JSON objectForKey@"args"] I get:

(
    {
        message = mymessage;
        username = myusername;
    }
)

I think the parenthesis are breaking it, don't know where they came from, help appreciated.

EDIT:

Thanks to chosen answer, here's the code I used to get the args keys.

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [packet.data dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: nil];
NSArray *argsArray = [[NSArray alloc] initWithArray:[JSON objectForKey:@"args"]];
NSDictionary *argsDict = [[NSDictionary alloc] initWithDictionary:[argsArray objectAtIndex:0]];
NSLog(@"keys = %@", [argsDict allKeys]);

1 Answer 1

2
[json objectForKey:@"args"]

is an NSArray. You can't initialize an NSDictionary directly with an array.

(in JSON, { and } denote key-value pairs, like NSDictionary, whereas [ and ] delimit ordered lists like NSArray, that's why the mapping is done as it is...)

Sign up to request clarification or add additional context in comments.

Comments

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.