首页 > 解决方案 > 按键将 JSON 反序列化为 C# 对象

问题描述

我正在寻找一种反序列化 JSON 的方法,但有些键应该是值,这就是它变得棘手的地方。我喜欢反序列化为:

int valuetoExtract1
int valuetoExtract2
int valuetoExtract3

JSON 看起来像这样:

{
    "option1": "someOption1",
    "option12": "someOption2",
    "id": [
        "12345"
    ],
    "filter": "someFilter",
    "actualValues": [
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract1": "123"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract2": "234"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract3": "345"
        }
    ]
}

标签: c#jsondeserialization

解决方案


你能试试这个:

public class ActualValue
{
    public int otherId { get; set; }
    public int id { get; set; }
    public string valuetoExtract1 { get; set; }
    public string valuetoExtract2 { get; set; }
    public string valuetoExtract3 { get; set; }
}

public class RootObject
{
    public string option1 { get; set; }
    public string option12 { get; set; }
    public List<string> id { get; set; }
    public string filter { get; set; }
    public List<ActualValue> actualValues { get; set; }
}

推荐阅读