user1247395
user1247395

Reputation: 419

iOS Parsing JSON and AFNetworking

I am consuming a web service using the AFNetworking tools like this:

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    [self GotJSONSuccess:JSON :response ];
} failure: nil ];

[operation start];

The web service responds and gives me the following json:

[
{
    "statusid": 1,
    "statusdesc": "ASSIGNED"
},
{
    "statusid": 2,
    "statusdesc": "COMPLETED"
},
{
    "statusid": 3,
    "statusdesc": "IN TRANSIT"
},
{
    "statusid": 4,
    "statusdesc": "DELAYED"
},
{
    "statusid": 5,
    "statusdesc": "ON HOLD"
}
]

I am using the following to attempt to parse the json:

- (void)GotJSONSuccess: (NSString*) JSON : (NSHTTPURLResponse*) response
{

NSString *newString = [NSString stringWithFormat:@"%@",JSON];

NSLog(@"response: %@", JSON);
NSData* data = [newString dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if (error){
    NSLog(@"error is %@", [error localizedDescription]);
    return;
}

NSArray *keys = [jsonObjects allKeys];

for (NSString *key in keys){
    NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
}
}

However the code falls into the error block and the output is "The operation couldn’t be completed. (Cocoa error 3840.)"

WHat am I doing wrong parsing this simple json?

Is there a better parsing approach than the one I am taking?

I would like to stick with the native iOS classes and methods for parsing if possible.

Upvotes: 2

Views: 9870

Answers (3)

mavericks
mavericks

Reputation: 1589

NSArray *array = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *arrStatusid = [array valueForKey:@"statusid"];

Try this with new version(2nd) of AFNetworking:

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

    //data is in response

} failure:^(AFHTTPRequestOperation *operation, NSError *err){

    //some code on failure
}];
[operation start];

Upvotes: 0

Jesse Rusak
Jesse Rusak

Reputation: 57168

If you look in FoundationErrors.h (or just search your project, including linked frameworks, for 3840, which is what I did) you'll see that the error corresponds to NSPropertyListReadCorruptError. Googling around for this suggests that your JSON might be invalid. Perhaps you have additional space characters before or after your JSON? Can you post the URL or the complete response you're getting from the server?

And what is kNilOptions?

Upvotes: 0

Siddharth Dhingra
Siddharth Dhingra

Reputation: 456

You can use NSJSONSerialization class to create an array as such

NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];

And then for each of your status ids you would just index into the array and pull out the dictionary as such:

NSDictionary *dict = [arr objectAtIndex:0];

That last line would pull out:`

{
    "statusid": 1,
    "statusdesc": "ASSIGNED"
}

And then you can use methods like object for key on the dictionary.`

Upvotes: 4

Related Questions