1

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());
    }
4
  • 2
    Deserialize as 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 name Commented Jun 16, 2022 at 2:24
  • thank you for getting back to me. please see the Edit1 section Commented Jun 16, 2022 at 2:44
  • I have shared away that works. is HttpClient.GetStringAsync safe to use compared to HttpClient.GetFromJsonAsync? HttpClient.GetFromJsonAsync does not work for some reason :/ Commented Jun 16, 2022 at 2:59
  • 1
    you could also try ReadAsStreamAsync is a bit more efficient than GetStringAsync Commented Jun 16, 2022 at 12:08

2 Answers 2

1

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;
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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());
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.