问题
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:
- The top level object is an NSArray or NSDictionary.
- All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
- All dictionary keys are instances of NSString.
- 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