c# - 如何使用 NewtonSoft JsonConvert 反序列化名称中带有破折号 (“-” 的属性?

标签 c# json json.net

我们有一个 JSON 对象,其中一个对象的名称中有一个破折号。例如下面。

{
    "veg": [
        {
            "id": "3",
            "name": "Vegetables",
            "count": "25"
        },
        {
            "id": "4",
            "name": "Dal",
            "count": "2"
        },
        {
            "id": "5",
            "name": "Rice",
            "count": "8"
        },
        {
            "id": "7",
            "name": "Breads",
            "count": "6"
        },
        {
            "id": "9",
            "name": "Meals",
            "count": "3"
        },
        {
            "id": "46",
            "name": "Extras",
            "count": "10"
        }
    ],
    "non-veg": [
        {
            "id": "25",
            "name": "Starters",
            "count": "9"
        },
        {
            "id": "30",
            "name": "Gravies",
            "count": "13"
        },
        {
            "id": "50",
            "name": "Rice",
            "count": "4"
        }
    ]
}

我们如何反序列化这个 json?

最佳答案

要回答有关如何使用 NewtonSoft 执行此操作的问题,您将使用 JsonProperty 属性属性标志。

[JsonProperty(PropertyName="non-veg")]
public string nonVeg { get; set; }

关于c# - 如何使用 NewtonSoft JsonConvert 反序列化名称中带有破折号 (“-” 的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14753113/

相关文章:

c# - 在 Sitecore 中无法访问 WebMethod

c# - IIS express 中未释放内存使用情况

c# - 对应C# Control.SuspendLayout/ResumeLayout的swing方法

php - 从 PHP 脚本获取 JSON

javascript - Nodejs - 循环 JSON 以查找并替换字符串

c# - 如何转换 JSON 对象中的转义 JSON 字符串?

c# - 带有嵌套对象的 JSON.NET CustomCreationConverter

asp.net-core - 使用自定义 JSON.NET JsonConverter 从 DescriptionAttribute 反序列化枚举停止工作

c# - 如何从流中读取 Access 文件 (.accdb)?

c# - c#.net 中用于 winforms 的气球类