Obj-C,在iTunes搜索查询字符串中对简体中文进行编码?
问题描述:
我很努力地使用带有简体中文的iTunes搜索API。我已经尝试了几个字符变体(由Google翻译提供)以及编码字符的HTML。Obj-C,在iTunes搜索查询字符串中对简体中文进行编码?
下面的代码不会导致任何错误,但它不会给我任何结果,不像英语单词。我也尝试了不同的中文单词。
但是我不确定如何继续。
NSString *url_string = @"";
NSString *keyword = [@"Chéngfǎ" stringByReplacingOccurrencesOfString: @" "
withString: @"%20"];
//NSString *keyword = [@"乘法" stringByReplacingOccurrencesOfString: @" "
withString: @"%20"];
url_string = [NSString stringWithFormat:
@"https://itunes.apple.com/search?term=%@&country=%@&entity=software", keyword, @"cn"];
NSError *error;
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: url_string]];
NSMutableArray *json;
if (data != nil)
{
json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error: &error];
}
NSDictionary *outer = [json valueForKey: @"results"];
for(NSDictionary *item in outer)
{
//results
}
答
我试图在浏览器中输入网址,并得到结果。我认为你需要对中文字符进行百分比编码?
https://itunes.apple.com/search?term=%E4%B9%98%E6%B3%95&country=cn&entity=software
您可以使用iOS的功能stringByAddingPercentEncodingWithAllowedCharacters
。
那么你的代码并不表明你有百分比编码它。您需要将“乘法”编码为“%E4%B9%98%E6%B3%95”。 – GeneCode
@Jules做到了吗? – GeneCode
是的,谢谢,接受 – Jules