iosobjective-cnsarraynsobject

Accessing data from an API response is iOS app


I have this iOS app I am working on and when I make a call to the API, I get this kind of data set back:

{
    "finished_on" = 1414499584;
    id = 4370760;
    ip = "1.1.1.1";
    mode = 1;
    name = asdasdasdasd1;
    remaining = 420;
    score = 0;
    "simulation_block" = 0;
    "simulation_id" = 0;
    "simulation_type" = 0;
    "started_on" = 1414499583;
    status = "-1";
    step = 1;
    total = 5;
}

The API call uses this:

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:req];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

And the response is coming from [responseObject description].

This all works and works everywhere else in my app. No problem there. Just confused on how I can work with this data for some reason.

Can anyone tell me how I can access parts of this data? For example, I'm wanting to pull out the "name". When I inspect the data in XCode, it's telling me it is a NSObject. And I've tried converting that down to a JSON string and other ways, but I'm having no luck.


Solution

  • You're using the description directive, which outputs an NSString representation of the data contained by that object. You need to access the actual contents of the object returned into responseObject with something akin to:

    responseObject[@"name"];
    

    ... but the implementation depends on exactly how AFNetworking is returning your responseObject.