2015-06-25 31 views
0

嗨,我得到了解析嵌套JSON,它有很多級別。我能夠獲得一級價值。如何在解析的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------> 

完全解析的JSON LINK

我對parsing-

-(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; 
+0

它不回答你的問題,但你沒有對你創建的MyCategory對象做任何事情。它需要在某處保存(addObject)。 – CharlesA

回答

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往往微不足道

@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,所以可能有些語法錯誤:)

+0

我可以看到結果再次包含數組和字典和值再次嵌套在數組和字典。所以我必須直接解析或再次創建內部json的模型。請讓知道 – ChenSmile

+0

結果已經解析並且只包含'MyCategory'對象。所以你可以使用它。 – arturdev

+0

是讓我檢查一次做,我會接受 – ChenSmile