0

有没有办法在JSON字符串中发送一些特殊字符,如'"如果我​​尝试在 JSON 中发送这个字符,那么我无法获得成功响应

这是代码:

NSMutableDictionary *dicWebServiceData = [[NSMutableDictionary alloc] init];
[dicWebServiceData setValue:@"Today's menu" forKey:@"description"];
NSString *strBulkData = [NSString stringWithFormat:@"bulk_data=[%@]",[dicWebService JSONRepresentation]];
strBulkData = [strBulkData stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];


NSData *postData = [strBulkData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",webServiceURL,strWebServiceName]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setTimeoutInterval:6000];
[req setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
[req setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:postData];

有没有办法在 JSON 中传递这个字符?谢谢,

4

1 回答 1

0

您不能在不转义回车的情况下将字符串文字包装在多行上。您应该使用 \ 使其成为多行字符串。希望能帮助到你。

例如

NSString *size = [Content stringByEvaluatingJavaScriptFromString:@"return     Subject.min(\
document.body.scrollHeight, document.documentElement.scrollHeight,\
document.body.offsetHeight, document.documentElement.offsetHeight,\
document.body.clientHeight, document.documentElement.clientHeight\
);"];
于 2013-08-27T04:58:52.747 回答