NSJSONSerialization isValidJSONObject returns false for received data from Venue search endpoint

南笙酒味 提交于 2019-12-11 06:14:43

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/40635993/nsjsonserialization-isvalidjsonobject-returns-false-for-received-data-from-venue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!