c# - 将硬编码的 JSON 字符串存储到变量

标签 c# json

我在将此 json 字符串存储到变量时遇到问题。我在这里想念的东西一定很愚蠢

private string someJson = @"{
    "ErrorMessage": "",
    "ErrorDetails": {
        "ErrorID": 111,
        "Description": {
            "Short": 0,
            "Verbose": 20
        },
        "ErrorDate": ""
    }
}";

最佳答案

如果您使用 @ 符号,则必须转义 "'s,它不允许在第一个 "之后将\用作转义符。所以这两个选项是:

不要使用 @ 并使用\来转义 "

string someJson = "{\"ErrorMessage\": \"\",\"ErrorDetails\": {\"ErrorID\": 111,\"Description\":{\"Short\": 0,\"Verbose\": 20},\"ErrorDate\": \"\"}}";

或者使用双引号

string someJson =@"{""ErrorMessage"": """",""ErrorDetails"": {""ErrorID"": 111,""Description"": {""Short"": 0,""Verbose"": 20},""ErrorDate"": """"}}";

关于c# - 将硬编码的 JSON 字符串存储到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998177/

相关文章:

c# - Azure服务总线: wait until all subscribers processed a message

c# - ASP.Net Core 2.2 读取 appsettings.json

javascript - 勾选复选框时如何更改 JSON 键的值?

javascript在连接变量中运行函数

json - 解码不一致的 JSON

IEnumerable<T> 与 <T> 的 C# 方差问题

c# - C# 中用于文本框控件的 selectall() 属性

c# - 存储过程结构

java - Gson使用TypeAdapter或Json Deserializer将数据从错误列表转换为空列表

JavaScript, promise {<pending>}