简体   繁体   中英

Deserialize JSON with null values in array

I have the following JSON array, that can also hold null values for some of the elements in the var array. The elements in the var array are always fix.

 "vars": [
        {
            "varCol": [
                {
                    "visible": false, 
                    "val": 1, 
                }, 
                {
                    "visible": false, 
                    "val": 5, 
                }, 
                {
                    "visible": false, 
                    "val": 5, 
                }, 
                null
            ],
            "Type": "Type1"
            "UniqueId": "ID1"
        }, 
          {
            "varCol": [
                {
                    "visible": true, 
                    "val": 1, 
                }, 
                null,
                {
                    "visible": false, 
                    "val": 5, 
                }, 
                null
            ],
            "Type": "Type2", 
            "UniqueId": "ID2"
        } 
        ]

I have the following C# deserializer classes:

public class Var
{
    public int VarId { get; set; }
    public string Type { get; set; }
    public List<VarCol> VarCols { get; set; }
}
public class VarCol
{
    public int VarColId { get; set; }
    public bool Visible { get; set; }
    public float Val { get; set; }
}

My desired output here is to have an entry in the VarCol that always holds the fixed structure of values in the array. In this case 4 entries in the varCol array for each vars element. For the deserialization of the JSON I am using:

Var v = JToken.Parse(json_string).ToObject<Var>();

I wanted to insert this as a comment but it is too long so I have to post it as an answer.

Pay attention to commas because your JSON is not valid. I think you want to have something like this:

    {
    "vars": [
        {
            "varCol": [
                {
                    "visible": false,
                    "val": 1
                },
                {
                    "visible": false,
                    "val": 5
                },
                {
                    "visible": false,
                    "val": 5
                },
                null
            ],
            "Type": "Type1",
            "UniqueId": "ID1"
        },
        {
            "varCol": [
                {
                    "visible": true,
                    "val": 1
                },
                null,
                {
                    "visible": false,
                    "val": 5
                },
                null
            ],
            "Type": "Type2",
            "UniqueId": "ID2"
        }
    ]
}

Have a nice day,

Alberto

Not sure what are you trying to achieve, because JToken.Parse() handles null values pretty much the same way as any other object value, so varCol will always have 4 elements.

Here's some test code: http://dotnetfiddle.net/9gGdH9

Sorry, my question was misleading. The problem I was facing was saving to the DB through Entity Framework a null VarCol in the Var collection. I needed this to be able to know which of the elements in the collection is null, since I have a fixed size list.I solved this by instantiating the null VarCol with an empty VarCol as also Alex Skalozub indicated in one of his comments:

It depends on the way your object is stored in the database. If VarCol has a many-to-one relation with Var, EF will probably store reference to Var record in VarCol table. But null record cannot be saved to database. That's why reading back will bring only those records that were saved. You probably need to do some post-processing after parsing JSON to replace null values with empty records before saving to DB. – Alex Skalozub 5 hours ago

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM