I want to clean strings that are retrieved from a database.
我想清理从数据库中检索的字符串。
I ran into this issue where a property value (a name from a database) had an embedded TAB character, and Chrome gave me an invalid TOKEN error while trying to load the JSON object.
我遇到了这个问题,其中属性值(来自数据库的名称)具有嵌入的TAB字符,并且Chrome在尝试加载JSON对象时给了我无效的TOKEN错误。
So now, I went to http://www.json.org/ and on the side it has a specification. But I'm having trouble understanding how to write a cleanser using this spec:
所以现在,我去了http://www.json.org/,并且它有一个规范。但我无法理解如何使用此规范编写清洁剂:
string
串
- ""
- “”
- " chars "
- “字符”
chars
字符
- char
- 烧焦
- char chars
- char chars
char
烧焦
- any-Unicode-character- except-"-or--or- control-character
- any-Unicode-character- except - “ - 或 - 或 - control-character
- \"
- \”
- \\
- \\
- /
- /
- \b
- \ b
- \f
- \F
- \n
- \ n
- \r
- \ r
- \t
- \ t
- \u four-hex-digits
- \ u四个十六进制数字
Given a string, how can I "clean" it such that I conform to this spec?
给定一个字符串,我如何“清理”它以便符合此规范?
Specifically, I am confused: does the spec allow TAB (0x0900) characters? If so, why did Chrome given an invalid TOKEN error?
具体来说,我很困惑:规范是否允许TAB(0x0900)字符?如果是这样,为什么Chrome会出现无效的TOKEN错误?
2 个解决方案
#1
2
This maybe what you are looking for it shows how to use the JavaScriptSerializer class in C#.
这可能就是你要找的,它展示了如何在C#中使用JavaScriptSerializer类。
How to create JSON String in C#
如何在C#中创建JSON字符串
#2
13
Tab characters (actual 0x09
, not escapes) cannot appear inside of quotes in JSON (though they are valid whitespace outside of quotes). You'll need to escape them with \t
or \u0009
(the former being preferable).
制表符(实际0x09,不是转义)不能出现在JSON中的引号内(尽管它们是引号之外的有效空格)。你需要用\ t或\ u0009(前者更可取)来逃避它们。
json.org says an unescaped character of a string must be:
json.org说字符串的未转义字符必须是:
Any UNICODE character except " or \ or control character
除“或\或控制字符”之外的任何UNICODE字符
Tab counts as a control character.
选项卡计为控制字符。
#1
2
This maybe what you are looking for it shows how to use the JavaScriptSerializer class in C#.
这可能就是你要找的,它展示了如何在C#中使用JavaScriptSerializer类。
How to create JSON String in C#
如何在C#中创建JSON字符串
#2
13
Tab characters (actual 0x09
, not escapes) cannot appear inside of quotes in JSON (though they are valid whitespace outside of quotes). You'll need to escape them with \t
or \u0009
(the former being preferable).
制表符(实际0x09,不是转义)不能出现在JSON中的引号内(尽管它们是引号之外的有效空格)。你需要用\ t或\ u0009(前者更可取)来逃避它们。
json.org says an unescaped character of a string must be:
json.org说字符串的未转义字符必须是:
Any UNICODE character except " or \ or control character
除“或\或控制字符”之外的任何UNICODE字符
Tab counts as a control character.
选项卡计为控制字符。