I am doing an authentication by using AFNetworking like below
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// Parsing will be here
{
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"ERROR :jason is %@",JSON);
}];
[client enqueueHTTPRequestOperation:operation];
Below is a JSON which received from server
{
"first_name" = A;
"last_name" = B;
}
Question :
How can a parse this JSON in ios. I am stuck because the return from server does not have any tag at all. If its format was
{
"user": {
"first_name": "A",
"last_name": "B",
}
}
I could parse by doing the following
NSArray *userList = [[NSArray alloc] init];
userList = [JSON objectForKey:@"results"];
Any ideas?
“Tag” is not in the terminology of JSON. The complex structure of JSON is negotiated between the sender and receiver (or simply dictated by the sender) and need not follow any particular outline, so long as it parses correctly.
The first quasi-JSON string you quoted would (if it were valid JSON) presumably identify the first and last name of an individual, and you would presumably know that it was a “user” identity, and what user it identified, from the context.
In general, you must approach a JSON string as an onion, peeling one layer at a time. In the case of your first string there is only one layer, an “Object” that maps to an NSDictionary. So, having received the object (and, if necessary, verified that it is indeed an NSDictionary using
isKindOfClass, you would cast theidvalue to anNSDictionaryand proceed to useobjectForKeyor some such to access the values within.