objective-c - iOS中遍历NSDictionary有null和重复是什么原因?

浏览:60日期:2023-11-21

问题描述

我在解析JSON的时候,到最后一步将字典内容遍历到model的时候就会出现null和重复数据,之前的每一步都是正确的,用朋友自用服务器的api测试有这个问题,我还用了百度APIStore里的新闻API,也是同样的问题,请问我是哪里做错了,请各位赐教,谢谢!

下面是代码,用的AFNetworking,JSON里有5个数组,最底下是控制台截图,用for循环和被注释掉的for in,都是一样的问题

[manager POST:@'地址不便透露,抱歉' parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {NSLog(@'成功');if ([responseObject isKindOfClass:[NSDictionary class]]) { NSArray *jData = [responseObject objectForKey:@'array']; //NSLog(@'%@',jData);for (NSDictionary * dict in jData) {NSArray * keys=[dict allKeys];JKey * jKey=[[JKey alloc]init];/*for (NSString * str in keys) { //对对象的属性一一赋值 KVC [jKey setValue:[dict objectForKey:str] forKey:str]; if ([dict objectForKey:str] > 0) {[self.testArray addObject:jKey]; }} */for (NSInteger i=0; i<keys.count; i++) { [jKey setValue:[dict objectForKey:keys[i]] forKey:keys[i]]; NSLog(@'jKey=%@',jKey.coverpath);} }}

objective-c - iOS中遍历NSDictionary有null和重复是什么原因?

问题解答

回答1:

自己试了一下,一一赋值就没问题了

for (NSDictionary * dict in jData){ JKey * jKey=[[JKey alloc]init]; jKey.title = [dict valueForKey:@'title']; jKey.link = [dict valueForKey:@'link']; jKey.Id = [dict valueForKey:@'id']; jKey.coverpath = [dict valueForKey:@'coverpath']; [self.testArray addObject:jKey];}回答2:

用MJExtension试一下for (NSInteger i = 0; i<array1.count; i++) {

NormalModel * model = [NormalModel objectWithKeyValues:array1[i]];[self.dataArray addObject:model];}

相关文章: