【问题标题】:How to do recursion parsing of Array of Multi-Level Dicitonary如何对多级字典数组进行递归解析
【发布时间】:2015-06-26 07:05:17
【问题描述】:

我在 JSON 响应中对数组的嵌套字典有共同的值。

我想在级别上实现它,以便所有递归都在循环内完成。

代码->

.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

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

}

结果数组再次包含嵌套级别的字典和数组。 我想以这种方式解析dict数组,所有主类别和子类别以及子类别都应该属于相应的对象。

例如

  • 电子 -> 手机配件-->耳机、充电器、电池等..... ----------等等---------

我的 JSON LINK

【问题讨论】:

  • 请解释清楚。你想要什么?。顺便说一句,好问题!
  • 对不起,你的问题是一团糟。除了您不想使用递归解析 JSON 响应之外,您的帖子中没有什么是清楚的
  • @LalitKumar 请查看更新的任务
  • @SergiiMartynenkoJr 你能看到更新的吗
  • 看看下面我的回答——这就是你所需要的。唯一的事情 - 从构造函数中删除代码,检查字典中的@“category” - 这不是正确的地方。而且您不会以这种方式创建产品。

标签: ios objective-c


【解决方案1】:

很难理解,你想要什么,但我会尽力回答

如果传入的字典是类别,您的递归方法将获取一些字典,创建其中描述的对象,并使用子字典调用自身

- (NSArray *) mapCategories:(NSArray*) categoriesDictArray {
    NSMutableArray *result = [NSMutableArray array];
    for(NSDictionary *objectDict in categoriesDictArray) {
        MyCategory *category = [MyCategory categoryWithDictionary:objectDict];
        [result addObject:category];
        if(objectDict[@"Category"]) {
           category. categories = [self mapCategories:objectDict[@"Category"]];    
        }
    }

    return result;
}

//place, where you get your first call
NSArray *mappedCategories = [self mapCategories:result[@"RESPONSE"][@"Category"] ];

希望你能明白

【讨论】:

  • 我对此感到困惑,我应该在使用我的构造函数中调用它
  • 不,不在构造函数中。在适当的地方使用它,您可以获得服务器响应
  • 这我真的不明白 MyProduct 。在我的情况下,它是 MyCategory ,你可以在回答时修改它吗 - (MyProduct ) mapProduct:(NSDictionary) productDict { MyProdcut *product = [MyProduct productWithDictionary:productDict];退货; }
  • 忘记 MyProduct - 看看更新的答案
  • NSMutableArray *result = [NSMutableDictionary 字典];我猜这不是将数组适当转换为可变字典的正确方法。请检查我是否收到错误。
猜你喜欢
  • 2017-07-01
  • 2022-11-03
  • 2019-06-04
  • 2021-10-20
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
相关资源
最近更新 更多