{"User":{"id":"42","name":"martin"}}将我的NSData转换为NSString会返回这个似乎完全有效的JSON,但是这个方法:
[NSJSONSerialization isValidJSONObject:data]说明这不是一个有效的JSON对象。
谁能指出我犯的一个错误,或者想出为什么会发生这样的事情?
发布于 2013-04-17 03:07:50
例如,我打赌字符串中有一个不可打印的字符,使数据无效。
声明一个NSError* error变量,然后调用[NSJSONSerialization JSONObjectWithData:data options:0 error:&error]方法来尝试转换JSON:显然,如果您的数据被认为是无效的,它将返回nil,但至少在那之后您将知道NSError* error变量中的错误描述。
NSData* data = ... // your data
NSError* error = nil; // Declare a variable to hold the error upon return
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // Try to convert your data
NSLog(@"obj: %@ ; error: %@", error); // Log the decoded object, and the error if anyhttps://stackoverflow.com/questions/16043953
复制相似问题