LS_
LS_

Reputation: 7129

How to parse specific Json Array

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?

Upvotes: 1

Views: 53

Answers (1)

Luca Davanzo
Luca Davanzo

Reputation: 21520

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]; 
        }
    }
}

Upvotes: 1

Related Questions