objective-c - iOS中遍历NSDictionary有null和重复是什么原因?
phpcn_u1582
phpcn_u1582 2017-05-02 09:34:28
0
2
719

我在解析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);
                }
            }
        }

phpcn_u1582
phpcn_u1582

membalas semua(2)
曾经蜡笔没有小新

Saya mencuba sendiri dan tiada masalah untuk memberikan nilai satu demi satu

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];
}
黄舟

Cuba gunakan MJExtension
untuk (NSInteger i = 0; i<array1.count; i++) {


     NormalModel * model = [NormalModel  objectWithKeyValues:array1[i]];
    
    [self.dataArray addObject:model];
}
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!