如何在解析的JSON中递归地解析类别对象

时间:2015-06-25 07:33:04

标签: ios objective-c xcode

您好我正在解析嵌套的JSON,它有很多级别。我能够达到第一级的价值。

我如何使用相同的模型类来使用递归来获取所有嵌套的JSON值。  我的JSON是 -

 {
"message": "Ok",
"STATUS_CODE": "200",
"REQUEST": {
    "userid": "12436124",
    "command": "GETCATEGORY"
},
"RESPONSE": {
    "Category": [
        {
            "type": "Tag",
            "categoryId": 11,
            "name": "Electronics",
            "catIconLeft": "",
            "catIconRight": "",
            "parentId": 0,
            "Category": [
                {
                    "type": "Category",
                    "categoryId": 84,
                    "name": "Mobile Accessories",
                    "parentId": 1,
                    "catIconLeft": "",
                    "catIconRight": "",
                    "Category": [
                        {
                            "type": "Product",
                            "categoryId": 90,
                            "name": "Headsets",
                            "catIconLeft": "",
                            "catIconRight": "",
                            "parentId": 9
                        },
                 <----so on------>

完整的Parsed JSON LINK

我的解析代码 -

-(void)call_CategoryListData{
 [params setObject:@"command" forKey:@"GETCATEGORY"];
 [params setObject:@"userid" forKey:@"12436124"];
 [serverCall actionmethod:Fake_Category parameters:params onComplete:^(NSMutableDictionary* results){

    if ([results isKindOfClass:[NSDictionary class]] || [results isKindOfClass:[NSMutableDictionary class]]){
        //DDLogVerbose(@"\n\n\n\n\nResult----->%@",results);
        NSMutableDictionary*responseDic=[results objectForKey:@"RESPONSE"];
        NSMutableArray*catArray=[responseDic objectForKey:@"Category"];

        for (NSMutableDictionary *dic in catArray) {
            NSMutableArray* tmp = [dic objectForKey:@"Category"];

            if (tmp) {
                MyCategory *cat = [[MyCategory alloc] init];
                cat.type = dic[@"type"];
                cat.categoryId = dic[@"categoryId"];

                if ([cat.type isEqualToString:@"Tag"]) {
                    cat.name = dic[@"name"];
                    cat.categoryId = dic[@"categoryId"];
                    [CatTag addObject:cat.name];
                    [CatID addObject:cat.categoryId];
                    <---------so on --------------->
                    NSLog(@"New Objects--->%@\n\n----->%@",CatTag,CatID);

                }
            }
        }
    }
 }
onError:^(NSError *error) {
// handle error here
}];
}

我的模特班 -

@interface MyCategory : NSObject
@property (nonatomic, strong) NSString  *type;
@property (nonatomic, strong) NSString  *name;
@property (nonatomic, strong) NSString  *categoryId;
@property(nonatomic,strong)   NSString  *catIconLeft;
@property (nonatomic,strong)  NSString  *catIconRight;
@property (nonatomic,strong)  NSString  *parentId;
@property (nonatomic, strong) MyCategory*Category;

1 个答案:

答案 0 :(得分:3)

MyCategory.h文件

@interface MyCategory : NSObject
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *categoryId;
@property (nonatomic, strong) NSString *catIconLeft;
@property (nonatomic, strong) NSString *catIconRight;
@property (nonatomic, strong) NSString *parentId;
@property (nonatomic, strong) NSArray  *categories;

- (id)initWithRootDictionary:(NSDictionary *)dictionary;
@end

MyCategory.m hile

@implementation
- (id)initWithRootDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    self.type = dictionary[@"type"];
    self.name = dictionary[@"name"];
    self.categoryId = dictionary[@"categoryId"];
    self.catIconLeft = dictionary[@"catIconLeft"];
    self.catIconRight = dictionary[@"catIconRight"];
    self.parentId = dictionary[@"parentId"];
    if (dictionary[@"category"]) {
        NSMutableArray *categories = [NSMutableArray new];
        for (NSDictionary *cat in dictionary[@"category"]) {
            MyCategory *category = [[MyCategory alloc] initWithRootDictionary:cat];
            [categories addObject:category];
        }
        self.categories = categories;
    }

    return self;
}
@end

// ...

-(void)call_CategoryListData
{ 
    //...
    NSMutableDictionary * responseDic = [results objectForKey:@"RESPONSE"];
    NSMutableArray *      catArray    = [responseDic objectForKey:@"Category"];

    NSMutableArray *result = [NSMutableArray new];
    for (NSDictionary *categoryDic in catArray) {
        MyCategory *category = [[MyCategory alloc] initWithRootDictionary:categoryDic];
        [result addObject:category];
    }

    // Do something with result
}

这是一个快速编写的代码,直接在这个编辑器中没有任何IDE,因此可能会出现一些语法错误:)