如何在JSON中添加像“'”这样的特殊字符

时间:2013-08-27 04:21:42

标签: iphone ios json web-services ios4

有没有办法在 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中传递这个角色? 谢谢,

1 个答案:

答案 0 :(得分:0)

如果不转义回车符,则无法将字符串文字包装在多行上。 您应该使用\来使其成为多行字符串。希望它有所帮助。

e.g。

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\
);"];