如何转义 UIWebView 的字符串?
我从服务器提取 json 数据。它包含一个字典,其中包含我插入到 html 模板中的文本。
如何正确转义该字符串?
NSString* json = /* can be anything, but also garbage */
NSString* json_escaped = [json someEscapeMethod]; /////// HOW TO ESCAPE THIS ?
NSString* script = [NSString stringWithFormat:@"process('%@')", json_escaped];
NSString* result = [self.webView stringByEvaluatingJavaScriptFromString:script];
我目前确实喜欢这样做,但我不确定转义是否足够
NSString* json_escaped = [json stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
I pull json data from a server. It contains a dictionary with text that I insert into a html template.
How do I properly escape this string?
NSString* json = /* can be anything, but also garbage */
NSString* json_escaped = [json someEscapeMethod]; /////// HOW TO ESCAPE THIS ?
NSString* script = [NSString stringWithFormat:@"process('%@')", json_escaped];
NSString* result = [self.webView stringByEvaluatingJavaScriptFromString:script];
I currently do like this, but I'm not sure wether the escaping is sufficiently
NSString* json_escaped = [json stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在就这样编码,但是开销很大。
并像这样用 javascript 对其进行解码
我仍在寻找开销更少的更好的解决方案。
更新
我最近了解到,存在多个用于桥接 Objective-C 和 javascript 的框架。
I now encode it this way, but the overhead is huge.
And decode it in javascript like this
I'm still looking for a better solution with less overhead.
Update
I have recently learned that there exists several frameworks for bridging objective-c with javascript.
的“转义字符串中的字符”部分NSRegularExpression 可能有效。
The "Escaping Characters in a String" section of NSRegularExpression may work.