【发布时间】:2013-09-19 14:21:17
【问题描述】:
我知道 JSON 规范说对象是一组无序的名称/值对。但我真的需要将有序的 JSON 发送到网络服务。问题是:
在网络服务(我不知道或无法访问代码)上,有一个步骤可以将我的 JSON 转换为 SOAP(所有对象都必须订购)。
这是我尝试过的:
NSString *json = @"{'perf':1,'begin':'2013-07-17T15:35:33.659-03:00','end':'2013-08-01T15:35:33.659-03:00'}";
NSMutableURLRequest *request = [self requestWithMethod:@"POST"
path:@"/myPath" parameters:nil];
//set headers
NSString *contentType = [NSString stringWithFormat:@"application/json"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request addValue:@"any-value" forHTTPHeaderField: @"User-Agent"];
//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[json dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation,
id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: [%@]",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
}];
[operation start];
那么,我该怎么做呢?
【问题讨论】:
-
对不起,它不计算。根据定义,JSON“对象”条目是无序的。任何要求它们以特定顺序呈现的网站都被严重破坏了。 (当然,数组条目是有序的,但这对任何人来说都不是问题。)
-
PS:如果硬编码 JSON 字符串对您不起作用,我怀疑您误解了问题并且还有其他问题。
-
PPS:这样做的目的是什么:
[NSString stringWithFormat:@"application/json"];? -
对 JSON 字符串进行硬编码有效。网络服务有问题。他正在尝试将我的 JSON 转换为 SOAP
-
如果网络服务不接受有效的 JSON,我会怀疑它的可靠性,当然不会信任它与金钱或隐私有关的任何事情。
标签: ios json web-services afnetworking