将 NSData 从 NSURLSession 转换为 JSON
Convert NSData from NSURLSession to JSON
我知道有许多类似的 SO 问题与我的标题相似。我已经检查过了,但仍然 运行 遇到这个问题。
我正在尝试访问 API,其中 return 是一个 should/could 格式为 JSON 的字符串。
要检索此字符串作为将字符串转换为 JSON 我正在使用(未成功)此代码:
NSError *error;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",responseString);
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSLog(@"%@",[json objectForKey:@"ID"]);
NSLog(@"Json: %@",json);
}];
[task resume];
NSLog(@"Response:...) return 是一个字符串,当我将其输入该网站时:http://jsonviewer.stack.hu 确认该字符串有效 JSON。
两个应该 return 一个 JSON 值的 NSLog 返回 null。
我尝试了什么:
NSDictionary *json = [NSJSON序列化JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
错误:&error];
NSDictionary *json = [NSJSON序列化JSONObjectWithData:data
options:NSJSONReadingMutableContainers
错误:&error];
我现在也试过了:
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSMutableDictionary *jsonObject;
NSError *err = nil;
@try
{
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
@catch (NSException *exception)
{
NSLog( @"Exception caught %@",exception);
}
NSDictionary *info = jsonObject;
NSLog(@"Json: %@",info);
}];
[task resume];
我在这里做错了什么?如何获得 NSDictionary (JSON) 结果。
试试这个:
NSMutableDictionary *jsonObject;
NSError *err = nil;
@try
{
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
@catch (NSException *exception)
{
NSLog( @"Exception caught %@",exception);
}
NSDictionary *info = jsonObject;
您的主要问题:
如果你读了+JSONObjectWithData:options:error:
的error参数,它会告诉你这个:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start
with array or object and option to allow fragments not set."
UserInfo={NSDebugDescription=JSON text did not start with array or
object and option to allow fragments not set.}
如前所述,您的答案如下所示:aStringKey = realAndValidJSONSurroundedByCurvyBrackets
,这不是有效的 JSON。
在聊天中讨论后,你与服务器端有联系,他们应该负责给予适当的JSON。在他们修复它之前,为了继续工作,您可以这样做:
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
str = [str stringByReplacingCharactersInRange:NSMakeRange(0, [@"aStringKey = " length] ) withString:@""];
NSError *jsonError = nil;
NSDictionary *jsonFinal = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];
if (jsonError)
{
NSLog(@"Error: %@", jsonError);
}
但请记住,那是 "quick hack/fix",不应保留在最终版本中并尽快删除。
您尝试过:
NSError *err = nil;
@try
{
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
@catch (NSException *exception)
{
NSLog( @"Exception caught %@",exception);
}
@try{}@catch(NSException *exception)
不应该工作,因为 +JSONObjectWithData:options:error:
不应该在你的情况下抛出 NSException
,所以理论上,没有什么可捕捉的,但它可能仍然不起作用(因为有 NSError
)。
当然,因为 data
参数应该是非空的,如果它是 nil
,你会得到一个 NSException
(这会记录 Exception caught data parameter is nil
),但这是另一个问题并且不会如果没有异常,我不能向你保证解析出错了(因为在我们的例子中是无效的 JSON)。
我知道有许多类似的 SO 问题与我的标题相似。我已经检查过了,但仍然 运行 遇到这个问题。
我正在尝试访问 API,其中 return 是一个 should/could 格式为 JSON 的字符串。
要检索此字符串作为将字符串转换为 JSON 我正在使用(未成功)此代码:
NSError *error;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",responseString);
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSLog(@"%@",[json objectForKey:@"ID"]);
NSLog(@"Json: %@",json);
}];
[task resume];
NSLog(@"Response:...) return 是一个字符串,当我将其输入该网站时:http://jsonviewer.stack.hu 确认该字符串有效 JSON。
两个应该 return 一个 JSON 值的 NSLog 返回 null。
我尝试了什么:
NSDictionary *json = [NSJSON序列化JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers 错误:&error];
NSDictionary *json = [NSJSON序列化JSONObjectWithData:data options:NSJSONReadingMutableContainers 错误:&error];
我现在也试过了:
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSMutableDictionary *jsonObject;
NSError *err = nil;
@try
{
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
@catch (NSException *exception)
{
NSLog( @"Exception caught %@",exception);
}
NSDictionary *info = jsonObject;
NSLog(@"Json: %@",info);
}];
[task resume];
我在这里做错了什么?如何获得 NSDictionary (JSON) 结果。
试试这个:
NSMutableDictionary *jsonObject;
NSError *err = nil;
@try
{
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
@catch (NSException *exception)
{
NSLog( @"Exception caught %@",exception);
}
NSDictionary *info = jsonObject;
您的主要问题:
如果你读了+JSONObjectWithData:options:error:
的error参数,它会告诉你这个:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
如前所述,您的答案如下所示:aStringKey = realAndValidJSONSurroundedByCurvyBrackets
,这不是有效的 JSON。
在聊天中讨论后,你与服务器端有联系,他们应该负责给予适当的JSON。在他们修复它之前,为了继续工作,您可以这样做:
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
str = [str stringByReplacingCharactersInRange:NSMakeRange(0, [@"aStringKey = " length] ) withString:@""];
NSError *jsonError = nil;
NSDictionary *jsonFinal = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];
if (jsonError)
{
NSLog(@"Error: %@", jsonError);
}
但请记住,那是 "quick hack/fix",不应保留在最终版本中并尽快删除。
您尝试过:
NSError *err = nil;
@try
{
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
@catch (NSException *exception)
{
NSLog( @"Exception caught %@",exception);
}
@try{}@catch(NSException *exception)
不应该工作,因为 +JSONObjectWithData:options:error:
不应该在你的情况下抛出 NSException
,所以理论上,没有什么可捕捉的,但它可能仍然不起作用(因为有 NSError
)。
当然,因为 data
参数应该是非空的,如果它是 nil
,你会得到一个 NSException
(这会记录 Exception caught data parameter is nil
),但这是另一个问题并且不会如果没有异常,我不能向你保证解析出错了(因为在我们的例子中是无效的 JSON)。