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

本文是小编为大家收集整理的关于NSJSONSerialization isValidJSONObject对于从Venue search端点收到的数据返回错误。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

xcode 8.1部署目标iOS 9.0

我从...

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

当我使用...

检查 data 对象时
if ([NSJSONSerialization isValidJSONObject:data])

我得到一个错误.

有人可以告诉我这里有什么问题吗?

编辑: 这是完整的if块(在块中添加typecast添加到 data 之后)...

    id foundationObject;

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

早些时候,代码没有IF块.只是...

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

当我意识到(在上述语句之后使用断点) FoundationObject data 不是.

注意:当我三月份为iOS 9.X运送应用程序时,这效果很好.可以称为场地端点的版本有所作为吗?

推荐答案

您在这里测试的是NSDATA. isValidJSONObject的输入IS id不是NSData

+ (BOOL)isValidJSONObject:(id)obj;

它返回YES如果可以将OBJ转换为JSON数据(NSData),则否则

另外,根据文档,

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

  1. 顶级对象是nsarray或nsdictionary.
  2. 所有对象都是NSSTRING,NSNUMBER,NSARRAY,NSDICTIONARY或NSNULL的实例.
  3. 所有字典键都是NSString的实例.
  4. 数字不是Nan或Infinity.

调用IsvalidjsonObject:或尝试转换是确定是否可以转换为JSON数据的确定方法.

对于转换NSData为jsonobject,您可以使用以下代码

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失败,您可以根据此确定原因.

请参阅此/a>有关在Objective-C

中使用NSError对象的更多信息

其他推荐答案

您在这里使用错误的方法isValidJSONObject会告诉您JSON对象(ID)是否将转换为JSON数据.

按照 doc doc doc

返回一个布尔值,该值指示给定对象是否可以是 转换为JSON数据. 是的,如果可以将OBJ转换为JSON数据,否则否.

如果要检查数据,则应使用JSONObjectWithData:options:error:并检查是否零.

编辑

您需要首先将数据转换为NSDictionary或NSArray

NSMutableDictionary * dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

然后检查dict是有效的儿子还是不喜欢这个

if([NSJSONSerialization isValidJSONObject:dict]){
    NSLog(@"dict is a valid JSON");
}else{
    NSLog(@"dict is not valid JSON");
}

本文地址:https://www.itbaoku.cn/post/2090876.html

问题描述

Xcode 8.1 Deployment target iOS 9.0

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

When I check the data object using...

if ([NSJSONSerialization isValidJSONObject:data])

i get a false.

Can someone tell me what is wrong over here?

Edit: 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);
}

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

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

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

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?

推荐答案

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

+ (BOOL)isValidJSONObject:(id)obj;

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

Also, according to documentation,

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

  1. The top level object is an NSArray or NSDictionary.
  2. All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
  3. All dictionary keys are instances of NSString.
  4. Numbers are not NaN or infinity.

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

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)
}

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.

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

其他推荐答案

You are using a wrong method here isValidJSONObject will tell you whether JSON object (id) will be converted to JSON data or not.

As per the doc

Returns a Boolean value that indicates whether a given object can be converted to JSON data. YES if obj can be converted to JSON data, otherwise NO.

If you want to check Data then you should use JSONObjectWithData:options:error: and check if it is nil or not.

Edit

You need to first convert your Data to NSDictionary or NSArray like this

NSMutableDictionary * dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

then check if dict is a valid son or not like this

if([NSJSONSerialization isValidJSONObject:dict]){
    NSLog(@"dict is a valid JSON");
}else{
    NSLog(@"dict is not valid JSON");
}