1

I have a Json array like this:

{"Response":{"Token":"///","Name":"///","Surname":"///","Phone":"///","Street":"///","Interno":"///","PostalCode":"///","City":"///","Province":{"ID":"///","Code":"///","Name":"///"},"Email":"///@gmail.com"},"Error":false,"ErrorDetails":null}

How can I parse the values inside Response and inside Province using objective-c? I tried with the following code:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Create an array to store the locations
    if(_fproducts == nil)
    {
        _fproducts = [[NSMutableArray alloc] init];
    }

    // Parse the JSON that came in
    NSError *error;
    jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];


    // Loop through Json objects, create question objects and add them to our questions array
    for (int i = 0; i < jsonArray.count; i++)
    {
        NSDictionary *jsonElement = jsonArray[i];

        // Create a new location object and set its props to JsonElement properties
        LoginCredentials *newFProduct = [[LoginCredentials alloc] init];
        newFProduct.token = jsonElement[@"Id"];
        newFProduct.nomeUser = jsonElement[@"nome"];

        NSLog(@"TOKEN:%@", newFProduct.token);
        NSLog(@"NOME:%@", newFProduct.nomeUser);

        // Add this question to the locations array
        [_fproducts addObject:newFProduct];
    }

    // Ready to notify delegate that data is ready and pass back items
    if (self.delegate)
    {
        [self.delegate itemsDownloaded:_fproducts];
    }
}

But I can't parse the values inside Response.. Do i need to create an array of Response and then parse it?

1
  • That is a dictionary. Not an array. Commented Apr 20, 2015 at 15:36

1 Answer 1

1

Convert all in NSDictionary, not in NSArray, and then:

jsonDictionary = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

if(!error) {
    NSDictionary* response = jsonDictionary[@"response"];
    if(response) {
        NSDictionary* province = response[@"province"]; 
        NSLog("Province: %@", province);
        /* here you can save all your values */
        if(province) {
            NSString* identificator = province[@"ID"];
            NSString* code = province[@"Code"];
            NSString* name = province[@"Name"];   
        }
    }
}

An elegant way to do this is creating your custom interface:

Province.h

@interface Province : NSObject 

- (id)initWithDictionary:(NSDictionary*)dictionary;

- (nonatomic, strong) NSString* identificator;
- (nonatomic, strong) NSString* code;
- (nonatomic, strong) NSString* name;

@end

Province.m

@implementation Province

- (id)initWithDictionary:(NSDictionary*)dictionary {
    self = [super init];
    self.identificator = dictionary[@"ID"];
    self.code = dictionary[@"Code"];
    self.name = dictionary[@"Name"];
    return self;
}

@end

So your code becomes:

if(!error) {
    NSDictionary* response = jsonDictionary[@"response"];
    if(response) {
        NSDictionary* provinceDictionary = response[@"province"]; 
        if(province) {
            Province* province = [Province initWithDictionary:provinceDictionary]; 
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot!! :) will try it as soon as I can
this worked perfectly thank you very much! Just one last question, what should I do if i want to parse the response inside error? I tried with the same method but error is not a dictionary so I'm stuck there..

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.