问题描述
- 解析JSON中的特殊字符
-
从 foursquare api抓取本地数据,然后将数据传递到webservice
添加给database
。现在我已经获取的当地数据为Mexico,其中有一些特殊字符一起的下列错误:
Unrecognized escape sequence. (13443):
使用下面的JSON编码:
NSString *requestString = [jsonstring UTF8String];
如何解析这些特殊字符?(比如西班牙 ?spanol)
解决方案
+(NSString *)http_post_method_changed:(NSString *)url content:(NSString *)jsonContent
{
NSURL *theURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
NSData *requestData = [jsonContent dataUsingEncoding:NSUTF8StringEncoding];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody: requestData];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *data=[[NSString alloc]initWithData:theResponseData encoding:NSUTF8StringEncoding];
NSLog(@"url to send request= %@",url);
NSLog(@"response1111:-%@",data);
return data;
}
用上面的代码就可以转换特殊字符了。
将你的url和json发送给它。
时间: 2024-10-03 00:03:12