NSJSONSerialization isValidJSONObject对于从Venue搜索端点接收到的数据返回false [英] NSJSONSerialization isValidJSONObject returns false for received data from Venue search endpoint

查看:121
本文介绍了NSJSONSerialization isValidJSONObject对于从Venue搜索端点接收到的数据返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 8.1部署目标iOS 9.0

Xcode 8.1 Deployment target iOS 9.0

我从...中的Foursquare Venue Search端点获得了一系列紧凑的场所对象.

I'm getting an array of compact venue objects as expected from Foursquare Venue Search endpoint in...

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data

当我使用...检查 data 对象时

When I check the data object using...

if ([NSJSONSerialization isValidJSONObject:data])

我弄错了.

有人可以告诉我这是怎么回事吗?

Can someone tell me what is wrong over here?

这是完整的if块(将类型转换添加到if块中的 data 之后)...

Here is the complete if block (after adding typecast to data in if block)...

    id foundationObject;

NSLog(@"data:- %@",data);
if ([NSJSONSerialization isValidJSONObject:(id)data])
{
    foundationObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"venues foundation object:- %@",foundationObject);
}

之前的代码没有if块.只是...

Earlier the code didn't have the if block. just...

id foundationObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

当我意识到(即使数据 不是)时, foundationObject 为零时(我在上面的语句之后使用断点)做出了更改.

The change was made when I realized (using breakpoint just after the above statement) that foundationObject was nil even though data wasn't.

注意:当我三月份发布适用于iOS 9.x的应用程序时,此方法运行良好.会场名称的版本会有所不同吗?

Note: this worked fine earlier when I shipped my app for iOS 9.x in march. Could the version of the Venue Endpoint being called be making a difference?

推荐答案

您要在此处测试的是NSData. isValidJSONObject的输入是id而不是NSData

What you're testing here is for NSData. The input for isValidJSONObject is id not NSData

+ (BOOL)isValidJSONObject:(id)obj;

如果obj可以转换为JSON数据(NSData),则返回YES,否则返回.

It returns YES if obj can be converted to JSON data (NSData), otherwise NO.

此外,根据文档,

可能转换为JSON的对象必须具有以下属性:

An object that may be converted to JSON must have the following properties:

  1. 顶级对象是NSArray或NSDictionary.
  2. 所有对象都是NSString,NSNumber,NSArray,NSDictionary或NSNull的实例.
  3. 所有字典键都是NSString的实例.
  4. 数字不是NaN或无穷大.

调用isValidJSONObject或尝试进行转换是确定给定对象是否可以转换为JSON数据的确定方法.

Calling isValidJSONObject: or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.

要将NSData转换为JSONObject,可以使用以下代码

For converting NSData to JSONObject, you can use the following code

NSError *error;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (!error) {
    // successfully done.
}else {
   NSLog(@"error: %@", error.localizedDescription)
}

请注意,要找出从服务器接收到的jsonData(NSData)出了什么问题,必须将NSError对象传递给上述代码中所示的方法.如果NSData到jsonObject的转换失败,则可以根据原因找出原因.

Please note that to find out what's wrong with jsonData(NSData) you're receiving from the server, you have to pass NSError object into the method as shown in the above code. If conversion of NSData into jsonObject fails, you can find out why according to that.

请查看此链接,了解有关在Objective-C中使用NSError对象的更多信息

Please look in to this link for more information on using NSError objects in Objective-C

这篇关于NSJSONSerialization isValidJSONObject对于从Venue搜索端点接收到的数据返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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