Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSdictionary不为Null,但显示0键/值_Iphone_Nsdictionary - Fatal编程技术网

Iphone NSdictionary不为Null,但显示0键/值

Iphone NSdictionary不为Null,但显示0键/值,iphone,nsdictionary,Iphone,Nsdictionary,我正在使用json从服务器获取数据。检查url是否收到点击响应,并在控制台中打印从json获取的所有值。但dictionary和Tryed with array也都通过断点显示空值,但当在控制台中打印时,显示数据被提取。下面是代码 NSString *urlStr = [NSString stringWithFormat:@"http://server39.pivbfg.com/360ads/apps/ads/%@/android/1360/ord0.9109502528132325?json=

我正在使用json从服务器获取数据。检查url是否收到点击响应,并在控制台中打印从json获取的所有值。但dictionary和Tryed with array也都通过断点显示空值,但当在控制台中打印时,显示数据被提取。下面是代码

NSString *urlStr = [NSString stringWithFormat:@"http://server39.pivbfg.com/360ads/apps/ads/%@/android/1360/ord0.9109502528132325?json=1&package_url=%@",self.mStrPid, base64String];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                            [NSURL URLWithString:
                             [urlStr stringByAddingPercentEscapesUsingEncoding:
                              NSUTF8StringEncoding]]];
NSLog(@"req-------------%@",request);

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

NSDictionary *json_dict = [json_string JSONValue];
NSLog(@"json_dict\n%@",json_dict);
NSLog(@"json_string\n%@",json_string);
NSMutableArray *arrAds = [[NSMutableArray alloc]init];
arrAds = [json_dict valueForKey:@"ads"];
请求进展顺利。 json_字符串是可以的。 但json_dict在控制台中打印值,但在断点处显示null。这可能是什么原因呢。一件事是我使用的是ARC,它是否会影响此代码。请为以上内容提供指导

这是错误:*由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类不符合密钥ads的键值编码要求。*第一次抛出调用堆栈: (0xb5012 0x13a9e7e 0x13dfb1 0xe565ed 0xdc28db 0xdc288d 0x613d 0x3d1707 0x3d1772 0x320915 0x320caf 0x320e45 0x329e57 0x5942 0x2ed697 0x2edc87 0x2EE8B 0x3001f5 0x30112b 0x2f2bd8 0x2202df9 0x2202ad0 0x2abf5 0x2a962 0x5BB6 0x5af44 0x5ae1b 0x2ee6ba 0x2f053c 0x544d 0x2B065) libc++abi.dylib:terminate调用引发异常

编辑代码

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSError *error;
    NSData *jsonData = [json_string dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSLog(@"results\n%@",results);
NSMutableArray *arrAds = [[NSMutableArray alloc]init];
arrAds = [results valueForKey:@"ads"];
NSLog(@"dict-------------%@",arrAds);

现在,阵列也出现了同样的问题。它正在控制台中打印值,但为空。

错误告诉您从JSON检索的属性不是字典,而是字符串。您的
json\u字符串
似乎不包含有效的json对象

在您的示例中,您也在泄漏:

NSMutableArray *arrAds = [[NSMutableArray alloc]init];
arrAds = [json_dict valueForKey:@"ads"];
创建一个新的
NSMutableArray
只是为了在下一行将一个新对象分配给同一个变量。此外,返回的对象不会是非可变版本。您可以将其替换为:

NSMutableArray *arrAds = [[json_dict objectForKey:@"ads"] mutableCopy];  

你试过这样做吗:
NSDictionary*json\u dict=(NSDictionary*)[json\u string JSONValue]
?我还发现这可能适用于您的问题:您的错误是消息发送到
NSCFString
实例,因此问题应该在
[json\u string JSONValue]
(它返回的是字符串而不是字典)可能响应不是有效的JSON。请验证此在线JSON验证程序门户网站www.jsonlint.com上的响应字符串“JSON_string”。我从@ss1271引用的链接中获得了解决方案。[1] :您在浏览器上的响应是什么>请发布
NSLog的输出(@“json\u string\n%@”,json\u string)看起来JSON无效。请发布您的JSON输出,现在无法检查代码是否有效!此外,您仍在泄漏
NSMutableArray
!您希望在输出中看到的是,解决了空字典的问题。我使用了NSJSONSerialization,就像我在发布
NSLog(@“json\u string\n%@”,json\u string)的输出之前所说的那样