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());
}
List<EveMarketerResponse>looks fine. Demo. Can you provide the code on how you deserialize it and specify what do you mean fails on the deserialization, get any exception or? Also may consider applying the JsonProperty attribute to match the JSON property nameHttpClient.GetStringAsyncsafe to use compared toHttpClient.GetFromJsonAsync?HttpClient.GetFromJsonAsyncdoes not work for some reason :/