I have the following JSON and class structure but I cannot figure out how to deserialize it into the objects.
The classes were automatically generated from the josn by https://app.quicktype.io/
The JSON was returned by an 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.
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. If you want to have it translated to an int, you have to do add a converter.
UPDATE : You can also declare the ContractId property as long. 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.
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.
I hope my solution will be useful, let me know if you have any doubts.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.