简体   繁体   English

如何将此 JSON 反序列化为 C# 对象

[英]How do I deserialize this JSON into C# objects

I have the following JSON and class structure but I cannot figure out how to deserialize it into the objects.我有以下 JSON 和类结构,但我不知道如何将它反序列化为对象。

The classes were automatically generated from the josn by https://app.quicktype.io/这些类是由https://app.quicktype.io/从 josn 自动生成的

The JSON was returned by an REST API. JSON 由 REST API 返回。

{
"last_executed_prices": {
    "9707645": [
        {
            "contract_id": "33837223",
            "last_executed_price": "79.37",
            "timestamp": "2020-03-04T14:33:14.711040Z"
        },
        {
            "contract_id": "33837246",
            "last_executed_price": "3.85",
            "timestamp": "2020-03-04T14:31:58.158066Z"
        },
        {
            "contract_id": "33837219",
            "last_executed_price": "36.5",
            "timestamp": "2020-03-04T14:33:10.361513Z"
        }
    ],
    "999567": [
        {
            "contract_id": "33837223",
            "last_executed_price": "79.37",
            "timestamp": "2020-03-04T14:33:14.711040Z"
        },
        {
            "contract_id": "33837246",
            "last_executed_price": "3.85",
            "timestamp": "2020-03-04T14:31:58.158066Z"
        },

        {
            "contract_id": "33837248",
            "last_executed_price": "7.69",
            "timestamp": "2020-03-04T14:32:41.560315Z"
        },
        {
            "contract_id": "33837220",
            "last_executed_price": "4.17",
            "timestamp": "2020-03-04T14:32:39.898192Z"
        }
    ]
}

} }

And this is the class structure:这是类结构:

    public partial class LastExecutedPrices
{
    public Dictionary<string, List<LastExecutedPrice>> LastExecutedPricesLastExecutedPrices { get; set; }
}

public partial class LastExecutedPrice
{
    public long ContractId { get; set; }
    public string LastExecutedPriceLastExecutedPrice { get; set; }
    public DateTimeOffset Timestamp { get; set; }
}

I've tried the following but just get a null object我已经尝试了以下但只是得到一个空对象

LastExecutedPrices data = JsonConvert.DeserializeObject<LastExecutedPrices>(badjson);

try this:尝试这个:

public partial class Root
{
    [JsonProperty("last_executed_prices")]
    public Dictionary<string, List<LastExecutedPrice>> LastExecutedPrices { get; set; }
}

public partial class LastExecutedPrice
{
    [JsonProperty("contract_id")]
    public long ContractId { get; set; }

    [JsonProperty("last_executed_price")]
    public string LastExecutedPriceLastExecutedPrice { get; set; }

    [JsonProperty("timestamp")]
    public DateTimeOffset Timestamp { get; set; }
}

The reason for which you have to create another class is that your JSON is an object that contains a field called last_executed_prices that is a dictionary that associates a string to a list of LastExecutedPrice.您必须创建另一个类的原因是您的 JSON 是一个包含名为 last_executed_prices 的字段的对象,该字段是一个将字符串关联到 LastExecutedPrice 列表的字典。

For the ContractId Property, you should change it to a string type because in your JSON it is surrounded by double-quotes, so it will be deserialized to a string.对于 ContractId 属性,您应该将其更改为字符串类型,因为在您的 JSON 中它被双引号包围,因此它将被反序列化为字符串。 If you want to have it translated to an int, you have to do add a converter.如果要将其转换为 int,则必须添加转换器。

UPDATE : You can also declare the ContractId property as long.更新:您也可以声明 ContractId 属性。 Newtonsoft.JSON is capable of deserializing the strings in the JSON into a long if that string is a valid number that can be parsed into an Int64.如果该字符串是可以解析为 Int64 的有效数字,则 Newtonsoft.JSON 能够将 JSON 中的字符串反序列化为 long。

You can easily test my solution by adding this:您可以通过添加以下内容轻松测试我的解决方案:

string json = @"{
    'last_executed_prices': {
        '9707645': [
            {
                'contract_id': '33837223',
                'last_executed_price': '79.37',
                'timestamp': '2020-03-04T14:33:14.711040Z'
            },
            {
                'contract_id': '33837246',
                'last_executed_price': '3.85',
                'timestamp': '2020-03-04T14:31:58.158066Z'
            },
            {
                'contract_id': '33837219',
                'last_executed_price': '36.5',
                'timestamp': '2020-03-04T14:33:10.361513Z'
            }
        ],
        '999567': [
            {
                'contract_id': '33837223',
                'last_executed_price': '79.37',
                'timestamp': '2020-03-04T14:33:14.711040Z'
            },
            {
                'contract_id': '33837246',
                'last_executed_price': '3.85',
                'timestamp': '2020-03-04T14:31:58.158066Z'
            },

            {
                'contract_id': '33837248',
                'last_executed_price': '7.69',
                'timestamp': '2020-03-04T14:32:41.560315Z'
            },
            {
                'contract_id': '33837220',
                'last_executed_price': '4.17',
                'timestamp': '2020-03-04T14:32:39.898192Z'
            }
        ]
    }
}";

Root obj = JsonConvert.DeserializeObject<Root>(json);

After the deserialization, you should have a valid object containing the dictionary that associates strings to List of LastExecutedPrice.反序列化之后,您应该有一个包含将字符串关联到 LastExecutedPrice 列表的字典的有效对象。

I hope my solution will be useful, let me know if you have any doubts.我希望我的解决方案有用,如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM