【发布时间】: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