嗨,我得到了解析嵌套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;
它不回答你的問題,但你沒有對你創建的MyCategory對象做任何事情。它需要在某處保存(addObject)。 – CharlesA