【发布时间】:2013-08-29 16:42:52
【问题描述】:
有什么方法可以在 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 中传递这个字符? 谢谢,
【问题讨论】:
-
来自官方页面json.org -> 字符串是零个或多个Unicode字符的序列,用双引号括起来,使用反斜杠转义。 (强调我的)
-
是的,json.org 会告诉你哪些字符需要转义。
-
@borrrden:谢谢你的建议。我对我们有帮助。 :)
标签: iphone ios json web-services ios4