gpt4 book ai didi

json.net 转义特殊字符

转载 作者:行者123 更新时间:2023-12-02 00:34:07 29 4
gpt4 key购买 nike

JSON 有点新所以请原谅这个简单的问题,但是根据找到的特殊字符列表 herehere , 单引号 (') 字符应该被转义。 specification还将斜线 (/) 视为可转义字符。为了说明,我正在使用文档示例(稍作修改)。

输入:

Product product = new Product();

product.Name = "O'Grady's Apples";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large", "X/Large" };

output = JsonConvert.SerializeObject(product, Formatting.Indented);

输出:

{    
"Name": "O'Grady's Apples",
"Expiry": "\/Date(1230422400000+0000)\/",
"Price": 3.99,
"Sizes": ["Small","Medium","Large", "X/Large"]
}

我希望看到的是

{  
"Name": "O\'Grady\'s Apples",...
"Sizes": ["Small","Medium","Large", "X\/Large"]
}

这是故意的吗?还是我误解了规范?

最佳答案

虽然可以在字符串中转义所有内容,但只有非常有限的一组需要转义:

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

因此只有 "\ 和不可打印的控制字符必须被转义。

关于json.net 转义特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5417344/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com