Reputation: 427
I am trying to deserialize this json array in a server side c# Blazor project:
[
{
"buy": {
"forQuery": {
"bid": true,
"types": [
16265
],
"regions": [
10000002
],
"systems": [
30000142
],
"hours": 24,
"minq": 1
},
"volume": 12177,
"wavg": 118125.12,
"avg": 118260.00,
"variance": 58000.00,
"stdDev": 240.83,
"median": 118100.00,
"fivePercent": 118466.60,
"max": 118600.00,
"min": 118000.00,
"highToLow": true,
"generated": 1655337615423
},
"sell": {
"forQuery": {
"bid": false,
"types": [
16265
],
"regions": [
10000002
],
"systems": [
30000142
],
"hours": 24,
"minq": 1
},
"volume": 935,
"wavg": 178386.31,
"avg": 194183.33,
"variance": 3731725666.67,
"stdDev": 61087.85,
"median": 140350.00,
"fivePercent": 134900.00,
"max": 250000.00,
"min": 134900.00,
"highToLow": false,
"generated": 1655337615423
}
}
]
For reference, if you'd like to see the json data on the source api, it's this: https://api.evemarketer.com/ec/marketstat/json?typeid=16265&usesystem=30000142
From all this information I only need: buy -> min, max sell -> min, max
I have created a class that contains these 2 properties:
public class BuySellStat
{
public double max;
public double min;
}
and would like to save them into this class:
public class EveMarketerResponse
{
public BuySellStat Buy;
public BuySellStat Sell;
}
I have tried many things, like deserializing into a Dictionary, List, Array... but cannot get it done. It always fails on the deserialization.
Would somebody please help me out here?
Thank you so much!
EDIT1:
I have tried it according to the sample Yong Shun shared, but get a Nullreference Exception in the first line of tthe foreach loop:
var list = HttpClient.GetFromJsonAsync<List<EveMarketerResponse>>("https://api.evemarketer.com/ec/marketstat/json?typeid=16265&usesystem=30000142").Result;
await JsConsoleLogger.LogAsync(list.Count.ToString()); //Will show in the browser console.
foreach (var response in list)
{
await JsConsoleLogger.LogAsync(response.Buy.min.ToString());
await JsConsoleLogger.LogAsync(response.Buy.max.ToString());
await JsConsoleLogger.LogAsync(response.Sell.min.ToString());
await JsConsoleLogger.LogAsync(response.Sell.min.ToString());
}
EDIT 2:
This one seems to work:
var responseJson = await HttpClient.GetStringAsync("https://api.evemarketer.com/ec/marketstat/json?typeid=16265&usesystem=30000142");
var responseList = JsonConvert.DeserializeObject<List<EveMarketerResponse>>(responseJson);
foreach (var response in responseList)
{
await JsConsoleLogger.LogAsync("buy min" + response.Buy.min.ToString());
await JsConsoleLogger.LogAsync("buy max" + response.Buy.max.ToString());
await JsConsoleLogger.LogAsync("sell min" + response.Sell.min.ToString());
await JsConsoleLogger.LogAsync("sell max" + response.Sell.max.ToString());
}
this one does not
var list = await HttpClient.GetFromJsonAsync<List<EveMarketerResponse>>("https://api.evemarketer.com/ec/marketstat/json?typeid=16265&usesystem=30000142");
foreach (var response in list)
{
await JsConsoleLogger.LogAsync("buy min" + response.Buy.min.ToString());
await JsConsoleLogger.LogAsync("buy max" + response.Buy.max.ToString());
await JsConsoleLogger.LogAsync("sell min" + response.Sell.min.ToString());
await JsConsoleLogger.LogAsync("sell max" + response.Sell.max.ToString());
}
Upvotes: 1
Views: 1166
Reputation: 427
Thanks to everyone for your support!
This one seems to work:
var responseJson = await HttpClient.GetStringAsync("https://api.evemarketer.com/ec/marketstat/json?typeid=16265&usesystem=30000142"); var responseList = JsonConvert.DeserializeObject<List>(responseJson);
foreach (var response in responseList)
{
await JsConsoleLogger.LogAsync("buy min" + response.Buy.min.ToString());
await JsConsoleLogger.LogAsync("buy max" + response.Buy.max.ToString());
await JsConsoleLogger.LogAsync("sell min" + response.Sell.min.ToString());
await JsConsoleLogger.LogAsync("sell max" + response.Sell.max.ToString());
}
Upvotes: 0
Reputation: 624
Try using NewtonSoft
public class BuySellStat
{
[JsonProperty("max")]
public double max;
[JsonProperty("min")]
public double min;
}
to deserialize :
public BuySellStat Deserialize<BuySellStat>(string result)
{
BuySellStat model = JsonConvert.DeserializeObject<BuySellStat>(result);
if (model == null)
{
return default;
}
return model;
}
Upvotes: 1