Home > Net >  Deserialize json to List<object> in C#
Deserialize json to List<object> in C#

Time:03-19

I have the following JSON string

    {
        "data": [
            {
                "symbol": "1COV.GE",
                "exposure": "0",
                "makerExposure": "-2028",
                "takerExposure": "2028",
                "makerPnl": "447.6688",
                "takerPnl": "-447.6688",
                "makerPositions": [
                    {
                        "name": "IB_001",
                        "position": "-2028",
                        "vwap": "47.41",
                        "pnl": "447.6688"
                    }
                ],
                "takerPositions": [
                    {
                        "name": "MT5_1",
                        "position": "2028",
                        "vwap": "47.41",
                        "pnl": "-447.6688"
                    }
                ]
            },
            {
                "symbol": "A",
                "exposure": "0",
                "makerExposure": "-10",
                "takerExposure": "10",
                "makerPnl": "-4.6",
                "takerPnl": "4.6",
                "makerPositions": [
                    {
                        "name": "IB_002",
                        "position": "-10",
                        "vwap": "136.78",
                        "pnl": "-4.6"
                    }
                ],
                "takerPositions": [
                    {
                        "name": "MT5_1",
                        "position": "10",
                        "vwap": "136.78",
                        "pnl": "4.6"
                    }
                ],
 "total": 2
            }
    }

And my goal is to serialize it into a List of object from the NODE "Data": I have the classes that map the data node fields:

 public class Positions
    {
        public string name { get; set; }
        public string position { get; set; }
        public string vwap { get; set; }
        public string pnl { get; set; }
    }

    public class ExPositions
    {
        public string symbol { get; set; }
        public string exposure { get; set; }
        public string makerExposure { get; set; }
        public string takerExposure { get; set; }
        public string makerPnl { get; set; }
        public string takerPnl { get; set; }
        public OZPositions makerPositions { get; set; }
        public OZPositions takerPositions { get; set; }
    }

Do you have any ideas how I can convert the node "data" to list of "ExPositions" objects, eg. List

I've did this but so far it throws an error

var positions = JsonSerializer.Deserialize<ExPositions>(json_string);

CodePudding user response:

There is an error in your json - it's missing a closing ] for the array (I'll assume it's a typo).

The real problem is that you need a wrapper class to represent the data node of the json which should contain a list (or array) of ExPositions. The makerPositions and takerPositions should also become lists (or arrays) too. Add the following class and update the position properties of ExPositions:

public class Data
{
    public List<ExPositions> data { get; set; }
}

// change positions to use a List too
public class ExPositions
{
    ...
    public List<Positions> makerPositions { get; set; }
    public List<Positions> takerPositions { get; set; }
}

Then you can deserialize using:

var result = JsonSerializer.Deserialize<Data>(json);

It's not clear where the ""total"": 2 property should be in your models (it's not clear in the json because of the issue I mentioned), you could add it to the Data class above (if it belongs there).

Online demo

CodePudding user response:

Try with:

public class Positions
    {
        public string name { get; set; }
        public string position { get; set; }
        public string vwap { get; set; }
        public string pnl { get; set; }
    }

    public class ExPositions
    {
        public string symbol { get; set; }
        public string exposure { get; set; }
        public string makerExposure { get; set; }
        public string takerExposure { get; set; }
        public string makerPnl { get; set; }
        public string takerPnl { get; set; }
        public Positions makerPositions { get; set; }
        public Positions takerPositions { get; set; }
    }

public class YourResult{
        public ExPositions data { get; set; }
        public int total { get; set; }
}

And then call:

var positions = JsonSerializer.Deserialize<YourResult>(json_string);

CodePudding user response:

As haldo mentioned, there is a typo in your JSON. To quickly parse and validate your JSON data, you can use any online JSON parsers to validate your JSON data. I usually use the chrome extension JSON Viewer Pro.

Also, in the link that haldo provided to the .NET Fiddle for the demo, there is a trailing comma in JSON data which JSON deserializers might not ignore.

Here is the link to the edited demo that haldo provided. Edited Demo

  • Related