0

I get this JSON from a web service:

{
    "Respons": [{
        "status": "101",
        "uid": "0"
    }]
}

I have tried to access the data with the following:

NSError* error;

//Response is a NSArray declared in header file.
self.response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSString *test = [[self.response objectAtIndex:0] objectForKey:@"status"]; //[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance
NSString *test = [[self.response objectForKey:@"status"] objectAtIndex:0]; //(null)

But none of them work, if i NSLog the NSArray holding the serialized data, this i what i get:

{
Respons = (
    {
        status = 105;
        uid = 0;
    }
);
}

How do i access the data?

user1359448
  • 1,539
  • 3
  • 14
  • 23
  • 2
    Read the error. The JOSN is a dictionary, not an array. – rmaddy Aug 27 '13 at 19:03
  • self.response should be an NSDictionary – random Aug 27 '13 at 19:05
  • See json.org. Learn how to read the JSON (it takes about 10 minutes to learn). Then understand how decoded JSON dumps with NSLog (almost like the original JSON, only `()` is used instead of `[]` for an array and not everything is quoted). – Hot Licks Aug 27 '13 at 19:55
  • possible duplicate of [Accessing JSON data inside an NSDictionary generated from NSJSONSerialization](http://stackoverflow.com/questions/8588264/accessing-json-data-inside-an-nsdictionary-generated-from-nsjsonserialization) – Hot Licks Aug 27 '13 at 19:56
  • @HotLicks Im perfectly capable of reading and writing JSON, have been using it for years in web development. – user1359448 Aug 29 '13 at 14:02
  • Then how come you can't figure out how to access the data? – Hot Licks Aug 29 '13 at 15:47

2 Answers2

3

Your JSON represents a dictionary, for whom the value associated with the Respons key is an array. And that array has a single object, itself a dictionary. And that dictionary has two keys, status and uid.

So, for example, if you wanted to extract the status, I believe you need:

NSArray *array = [self.response objectForKey:@"Respons"];
NSDictionary *dictionary = [array objectAtIndex:0];
NSString *status = [dictionary objectForKey:@"status"];

Or, in latest versions of the compiler:

NSArray *array = self.response[@"Respons"];
NSDictionary *dictionary = array[0];
NSString *status = dictionary[@"status"];

Or, more concisely:

NSString *status = self.response[@"Respons"][0][@"status"];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for the great answer, when i try to compare `status` with a string i get the following warning: Comparison of distinct pointer types 'NSString *' 'char *' Code: NSDictionary *test = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSArray *array = test[@"Respons"]; NSDictionary *dictionary = array[0]; NSString *status = dictionary[@"status"]; NSLog(@"%@", status); if (status == "100") { // } else { // } – user1359448 Aug 29 '13 at 14:08
  • @user1359448 Two problems: 1. You don't compare strings with `==` operator; you use `isEqualToString`. 2. You forgot the `@` in `@"100"`. Thus, it should be `if ([status isEqualToString:@"100"]) ...`. Either that or `if ([status integerValue] == 100) ...`, which converts the string to a number (avoiding hassles if there was a space in there or something weird like that). – Rob Aug 29 '13 at 15:22
1

Your top level object isn't an array, it's a dictionary. You can easily bypass this and add the contents of that key to your array.

self.response = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error] objectForKey:@"Respons"];
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281