如何解析特定的Json数组

时间:2015-04-20 15:20:53

标签: objective-c json

我有一个像这样的Json数组:

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

如何使用objective-c解析Response内和Province内的值? 我尝试使用以下代码:

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

但我无法解析Response中的值。我是否需要创建一个Response数组然后解析它?

1 个答案:

答案 0 :(得分:1)

转换所有NSDictionary,而不是NSArray,然后:

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

这样做的一个优雅方法是创建自定义界面:

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

所以你的代码变成了:

if(!error) {
    NSDictionary* response = jsonDictionary[@"response"];
    if(response) {
        NSDictionary* provinceDictionary = response[@"province"]; 
        if(province) {
            Province* province = [Province initWithDictionary:provinceDictionary]; 
        }
    }
}