iOS NSJSON返回Null [英] iOS NSJSON Returning Null

查看:60
本文介绍了iOS NSJSON返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

 hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];    

NSError *error = nil;
NSDictionary *result = [NSJSONSerialization 
                        JSONObjectWithData:dataURL
                        options:kNilOptions 
                        error:&error];

NSArray *files = [result objectForKey:@"GV_Return"];

NSLog(@"Files: %@", files);

// For Each Key we seperate the data and add it to an object to be used.
for (NSDictionary *file in files)
{           
NSString *userNumber = [result objectForKey:@"UserNumber"];
NSLog(@"output: %@", userNumber);
}

我遇到的问题是 NSLog(@"Files:%@",files); 返回我期望的结果

The problem I'm having is that NSLog(@"Files: %@", files); returns what I expect

Files: (
    {
    UserNumber = 1;
}
)

但是 NSLog(@"output:%@",userNumber); 返回 NULL

我需要帮助弄清楚为什么NSString * userNumber = [result objectForKey:@"UserNumber"];似乎不应该返回"1".

I need help figuring out why NSString *userNumber = [result objectForKey:@"UserNumber"]; does not seem to be returning "1" as it should.

推荐答案

将代码更改为:

for (NSDictionary *file in files)
{           
NSString *userNumber = [file objectForKey:@"UserNumber"];
NSLog(@"output: %@", userNumber);
}

file objectForKey ... ,而不是 result objectForKey ... .您要遍历 files 数组,出于for循环内代码的目的,将其中的每个条目标识为 file .

i.e. file objectForKey..., not result objectForKey.... You're iterating through the files array with each entry in it being identified as file for the purposes of the code within the for loop.

这篇关于iOS NSJSON返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆