user3026899 February 2016

how to parse this JSON in OBJ c

I receive this JSON string from a web process

{
   "result":"ok",
   "description":"",
   "err_data":"",
   "data":[
      {
         "id":"14D19A9B-3D65-4FE2-9ACE-4C2D708DAAD8"
      },
      {
         "id":"8BFD10B8-F5FD-4CEE-A307-FE4382A0A7FD"
      }
   ]
}

and when I use the following to get the data:

NSError *jsonError = nil;
NSData *objectData = [ret dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json= [NSJSONSerialization JSONObjectWithData: objectData options:kNilOptions error: &jsonError];

NSLog(@"data: %@",[json objectForKey:@"data"]);

it gets logged as:

(
    {
        id = "14D19A9B-3D65-4FE2-9ACE-4C2D708DAAD8";
    },
    {
        id = "8BFD10B8-F5FD-4CEE-A307-FE4382A0A7FD";
    }
)

How can I parse the data as an NSDictionary with value and keys?

Answers


danh February 2016

The web returns an object that has a property which is an array of objects, so...

NSDictionary *json=  // your code
NSArray *array = json[@"data"];
for (NSDictionary *element in array) {
    NSLog(@"%@", element);
    // or, digging a little deeper
    NSString *idString = element[@"id"];
    NSLog(@"id=%@", idString);
}

Post Status

Asked in February 2016
Viewed 2,688 times
Voted 8
Answered 1 times

Search




Leave an answer