0

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?

3
  • 1
    Well, your first JSON sequence is wrong. But I'm wondering if what you printed there is the output of NSDictionary.description, rather than the actual JSON. And I don't know what you mean by "tag" -- { "X" : "Y" } is perfectly valid JSON (as is 5). Commented Sep 17, 2012 at 22:09
  • 1
    And note that your code snippet doesn't "parse" anything. Presumably JSON is the parsed result of the JSON string and is, in both of the above cases, an NSDictionary. Commented Sep 17, 2012 at 22:19
  • the first one is the actual JSON. I m using Postman from Chrome and the result is returned like that.What I meant about tag is the name of the object. Therefore, in the second JSON, it is user.I agree that I have not parsed because given a NSDictionary, I dont know how to turn it into an array... because it does not have a key ( like the second one has a key named user so that I can do objectForKey to return an array... Commented Sep 17, 2012 at 22:34

2 Answers 2

2

"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 the id value to an NSDictionary and proceed to use objectForKey or some such to access the values within.

Sign up to request clarification or add additional context in comments.

1 Comment

you are right about the way looked at JSON as onion. I just gave it a try so that I could retrieve the first_name and the las_name. Thanks for your help
0

You should utilize the new JSONSerialization class available in iOS5 which makes JSON easy to consume. Below is a quick example grabbing your json and parsing it in the fetchedData method. Resources listed below

//CALL dataWithContentsOfURL ONCE DATA HAS BEEN RECEIVED YOU CAN PARSE THE JSON:
NSError *error = nil;
NSData* data = [NSData dataWithContentsOfURL:kURL options:NSDataReadingUncached error:&error];
if (error) {
    NSLog(@"%@", [error localizedDescription]);
} else {
    NSLog(@"Data has loaded successfully.");
}
//MORE CODE HERE?


- (void)fetchedData:(NSData *)responseData {
    _yourObjectArray = [[NSMutableArray alloc] init];

    //parse out the json data
    NSError* error;
    NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                    options:kNilOptions
                                                      error:&error];

    for(int i=0; i<[json count]; i++){
        YourObject *yObject = [[YourObject alloc]init];
        NSDictionary* rawData = [json objectAtIndex:i]; //2

        yObject.FirstName = [rawData objectForKey:@"first_name"];
        yObject.LastName = [rawData objectForKey:@"last_name"];

        [_yourObjectArray addObject:yObject];
    }
}

SOURCES: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html http://www.raywenderlich.com/5492/working-with-json-in-ios-5

1 Comment

sorry, if you look at the first JSON from OP, there is no tag for that kind of object at all. That is why I am stuck.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.