包含正斜杠/和HTML的字符串的NSJSONSerialization序列化未正确转义 [英] NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly

查看:200
本文介绍了包含正斜杠/和HTML的字符串的NSJSONSerialization序列化未正确转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些简单的HTML转换为JSON对象中的字符串值,但我无法使字符串编码无法转义NSJSONSerialization中的字符串。

I am trying to convert some simple HTML into a string value in a JSON object and I'm having trouble getting the string encoding to not escape the string in NSJSONSerialization.

示例...我有一个包含一些基本HTML文本的字符串:

Example... I have a string which contains some basic HTML text:

NSString *str = @"<html><body><p>Samples / Text</p></body></html>";

所需的结果是JSON,其中包含HTML值:

The desired outcome is JSON with HTML as the value:

{
    "Title":"My Title",
    "Instructions":"<html><body><p>Samples / Text</p></body></html>"
}

我正在使用标准技术将NSDictionary转换为包含JSON的NSString :

I'm using the standard technique to convert an NSDictionary to a NSString containing JSON:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:str forKey:@"Instructions"];
[dict setObject:@"My Title" forKey:@"Title"];

NSError *err;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&err];
NSString *resultingString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", resultingString);

此方法生成的JSON有效,但HTML包含所有正斜杠转义:

The JSON produced by this method is valid, however the HTML has all forward slashes escaped:

{
    "Title":"My Title",
    "Instructions":"<html><body><p>Samples \/ Text<\/p><\/body><\/html>"
}

这会在指令JSON字符串中创建无效的HTML。

This creates invalid HTML in the instructions JSON string.

我想坚持使用NSJSONSerialization,因为我们在框架中的其他地方都使用它,并且在切换到非Apple库之前我已经被烧毁了,因为它们得不到支持。我尝试了很多不同的字符串编码,并且所有字符串编码都逃脱了尖括号。

I'd like to stick with NSJSONSerialization since we're using that everywhere else in our framework and I've been burned before switching to non-Apple libraries as they get desupported. I've tried many different string encodings and all of them escape the angle brackets.

显然\ /是JavaScript中/字符的有效表示,这就是为什么正斜杠被转义(即使StackOverflow文本编辑器也将其转义)。请参阅:
使用正斜杠转义json字符串?
以及 JSON:为什么正斜杠被转义?。我只是不希望它这样做,并且似乎没有办法阻止iOS在序列化时转义字符串值中的正斜杠。

Apparently \/ is a valid representation in JavaScript for the / characters, which is why forward slash is escaped (even the StackOverflow text editor escaped it). See: escaping json string with a forward slash? and also JSON: why are forward slashes escaped?. I just don't want it to do that and there doesn't seem to be a way to stop iOS from escaping forward slashes in string values when serializing.

推荐答案

我相信 NSJSONSerialization 的行为与HTML编码一样。

I believeNSJSONSerialization is behaving as designed in regards to encoding HTML.

如果你看一些问题( 1 2 )在JSON编码HTML时你会看到答案总是提到转义正斜杠。

If you look at some questions (1, 2) on encoding HTML in JSON you'll see the answers always mention escaping the forward slashes.

JSON 不需要转发斜杠,但HTML不允许使用javascript字符串< / 因为它可能与< SCRIPT> 标记的结尾混淆。

JSON doesn't require forward slashes to be escaped, but HTML doesn't allow a javascript string to contain </ as it can be confused with the end of the <SCRIPT> tag.

查看答案此处这里,最直接的是 w3.org HTML4附录,载于 B.3.2指定非HTML数据

See the answers here, here and most directly the w3.org HTML4 Appendix which states in B.3.2 Specifying non-HTML data

ILLEGAL EXAMPLE: 
The following script data incorrectly contains a "</" sequence (as part of "</EM>") before the SCRIPT end tag:

<SCRIPT type="text/javascript">
  document.write ("<EM>This won't work</EM>")
</SCRIPT>

虽然此行为可能会导致您的问题 NSJSONSerialisation 只是按照古老的HTML数据编码规则进行播放,以便在< SCRIPT> 标签中使用。

Although this behaviour may cause issues for you NSJSONSerialisation is just playing by the age old rules of encoding HTML data for use in <SCRIPT> tags.

这篇关于包含正斜杠/和HTML的字符串的NSJSONSerialization序列化未正确转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆