0

So i have gotten my Json data in which i have got:

{
"name": "brad"
}

I am trying to store that individual name as a string.

@try
{
     NSError *error;
NSString *url_string = [NSString stringWithFormat: @"http://Share/scripts/newjson.json"];
///Dummy URL
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]];
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"json: %@", json);

}
@catch (NSException *e)
{
    NSLog(@"Internet not enabled");
    //something went wrong
    //populate the NSError object so it can be accessed
}

But when i print out the json file i just get what is shown above, do i need to use a delimiter or something to get this string ?

Thanks

0

1 Answer 1

2

Actually the JSON is a dictionary not an array and with nilOptions not mutable.

To get brad write

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *name = json[@"name"];
NSLog(@"name: %@", name);
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.