【问题标题】:How can I escape the *s and %s in this string without breaking the URL?如何在不破坏 URL 的情况下转义此字符串中的 *s 和 %s?
【发布时间】:2014-07-24 20:52:22
【问题描述】:

我这里有这个动作:

- (IBAction)searchButton:(id)sender {
NSString *textString = self.symbolSearchField.text;
NSURL *sourceURL = [[NSURL alloc]  initWithString: [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20yahoo.finance.quotes%%20where%%20symbol%%20in%%20%%28%%22%@%%22%%29&env=store://datatables.org/alltableswithkeys", textString]];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:sourceURL];
parser.delegate = self;
[parser parse];

我希望它从搜索字段中获取文本,使用该文本修改 URL,然后从 URL 解析 XML。但是,当我转义 *s 和 %s 时,URL 似乎会“损坏”,并且无法解析。如果我将 URL 保留原样而不转义...

    NSURL *sourceURL = [[NSURL alloc]  initWithString: [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22%@%22%29&env=store://datatables.org/alltableswithkeys", textString]];

...然后我收到“无效的转换说明符 '*'”和“比数据参数更多的 '%' 转换”警告。

所以我的问题是,如何在不破坏 URL 的情况下转义符号?

编辑

为了清楚起见,这是我在进行一些调整后的代码。

NSString *textString = self.symbolSearchField.text; NSString *query = [NSString stringWithFormat:@"select * from yahoo.finance.quotes where symbol in (\"%@\")", textString]; NSString *escapedStoreURL = [@"store://datatables.org/alltableswithkeys" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSString *escapedQuery = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSString *URLString = [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=%@&env=%@", escapedQuery, escapedStoreURL]; NSURL *sourceURL = [NSURL URLWithString:URLString]; NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:sourceURL]; parser.delegate = self; [parser parse];

【问题讨论】:

    标签: ios objective-c string parsing escaping


    【解决方案1】:

    试试这个:

    NSString *firstPart = @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22";
    NSString *lastPart = @"%22%29&env=store://datatables.org/alltableswithkeys";
    NSURL *sourceURL = [[NSURL alloc]  initWithString: [NSString stringWithFormat:@"%@%@%@", firstPart, textString, lastPart]];
    

    编辑

    您可能还想考虑以编程方式转义该查询,而不是事先在其中转义所有内容。商店 URL 也需要进行 URL 编码。

    NSString *query = [NSString stringWithFormat:@"select * from yahoo.finance.quotes where symbol in (\"%@\")", textString];
    NSString *escapedStoreUrl = [@"store://datatables.org/alltableswithkeys" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
    NSString *escapedQuery = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
    NSString *urlString = [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=%@&env=%@", escapedQuery, escapedStoreUrl];
    

    编辑 2

    于是我又回来看这个问题,结果发现yahoo.finance.quotes中的u和o和alltableswithkeys的两个L之间有一些不可见的字符(具体是两个U+200c)。删除这些应该可以解决您的问题。 原来是caused by StackOverflow

    【讨论】:

    • 仍然不解析。使用 NSLog 查看 URL 是否仍然损坏,这是输出:http://query.yahooapis.com/v1/public/yql?q=select220from2ahoo.finance.quotes2here Ãô¯7ö¯7+Éymbol 0n22SLA2&env=store://datatables.org/alltableswithkeys
    • 好的,有两件事:首先,看起来你只是做了 NSLog(string) 而不是 NSLog(@"%@", string),这就是百分号消失的原因。其次,您尝试在那里替换的字符串是什么?
    • 对,我很傻。现在的输出是http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&env=store://datatables.org/alltableswithkeys,它可以工作,但它仍然无法解析。我要替换的字符串是股票代码(例如 AAPL)。
    • @user3874680 也许商店 URL 也需要编码?我已经用一个例子更新了我的答案。
    • 您建议的两种方法似乎都可以生成有效的 URL,并且字符串到 URL 的转换都很好,但它仍然没有解析 XML。这是没有意义的,因为当我 NSLog sourceURL 时,它会记录它,但是当我尝试解析 sourceURL 时它什么也不做。
    【解决方案2】:

    您为什么不直接使用 NSMutableString,然后逐步附加字符串?

    NSString *textString = @"AAPL";
    NSMutableString *query = [[NSMutableString alloc] init];
    NSString *startString = @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22";
    NSString *endString = @"%22%29&env=store://datatables.org/alltableswithkeys";
    
    [query appendString:startString];
    [query appendFormat:@"%@",textString];
    [query appendString:endString];
    
    NSURL *sourceURL = [[NSURL alloc] initWithString:query];
    

    现在,变量 sourceURL 是带有“转义”符号的 URL。

    【讨论】:

    • @user3874680 这可能是因为变量query 是一个 NSMutableString——我已经用 NSURL 更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多