【发布时间】:2014-01-16 16:26:31
【问题描述】:
我有一个奇怪的错误,我无法理解。 我正在构建一个使用远程 JSON API 进行通信的应用程序。 我正在使用 AFNetworking 执行请求并遇到与序列化响应相关的问题。
当我使用浏览器调用我们的服务器时,我得到了正确的 json 响应:
{
{
"tip_uuid": "4067d78c-a222-4190-8d5b-a6822a38e5e6",
"deck_uuid": "c3c0854e-1f28-45ea-a643-c253b5c66a45",
"user_uuid": "fb1dff53-b97f-47f3-b4e7-6c2184b85466",
"latitude": "52.66",
"longitude": "4",
"categoryName": "Other",
"imageURL": "\/images\/dummy_300.png",
"averageRating": 3,
"distanceToLocation": "7596772",
"isUserAwesome": true
},
..... (100 more responses)
}
但是当我使用 AFNetworking 拨打同样的电话时
[编辑]
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"ACCEPT"];
[manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self processServerResponse:responseObject successBlock:success failureBlock:failure];
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLog(@"------- Failed url: %@ AFNetworking operation: %@. Responsestring: %@", URLString, [error description], operation.responseString);
showM2(NSLocalizedString(@"Network error", @"Network error title"), NSLocalizedString(@"Please try again later", @"network error message"));
failure(nil);
}];
我得到了回应: [/编辑]
{
{
averageRating = 0;
categoryName = Music;
"deck_uuid" = "c3c0854e-1f28-45ea-a643-c253b5c66a45";
distanceToLocation = 5718;
imageURL = "/images/tip/fb1dff53-b97f-47f3-b4e7-6c2184b85466_4dd49562817bc39f7567321c22469e7bf846f902_1389202937.png";
isUserAwesome = 0;
latitude = "52.3664193";
longitude = "4.8871613";
"tip_uuid" = "83d5a3f7-be60-4974-9347-67cadc307790";
"user_uuid" = "fb1dff53-b97f-47f3-b4e7-6c2184b85466";
},
....(100 more responses)
}
(最大的)问题是它以某种方式在几个键(tip_uuid、user_uuid 和 deck_uuid)周围添加了引号
这是怎么回事,我该如何解决?
【问题讨论】:
-
我用来发出请求的代码是:AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer 序列化器]; [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"ACCEPT"]; [manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { [self processServerResponse:responseObject successBlock:success failureBlock:failure]; }
-
不要在评论中发布不可读的代码,而是使用代码更新您的问题并删除评论。
-
您看到了 JSON 和 NSDictionary 的 NSLog 之间的区别。 NSDictionary 日志只引用带有“特殊”字符的键,而 JSON 引用所有内容。同样,正斜杠在 JSON 中转义,但不在 NSDictionary 日志中。没有什么问题,所以没有什么需要解决的。
-
嗯,这似乎是合乎逻辑的,因此未存储这些值的事实与这种显示差异无关。 Tnx!
-
@HotLicks:您可能想添加这个作为答案 - 这个主题可能不时对其他人有用......
标签: ios objective-c json afnetworking