2

我正在努力使用带有简体中文的 iTunes 搜索 API。我已经尝试了几个字符变体(由谷歌翻译提供)以及对字符进行 HTML 编码。

下面的代码不会导致任何错误,但它不会给我任何结果,这与英文单词不同。我也尝试过不同的中文单词。

但是我不确定如何进行。

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
}
4

1 回答 1

2

I tried to type the URL into browser and I get the results. I think you need to percent encode the chinese characters?

https://itunes.apple.com/search?term=%E4%B9%98%E6%B3%95&country=cn&entity=software

You can use iOS function stringByAddingPercentEncodingWithAllowedCharacters.

于 2017-09-08T00:17:16.673 回答