Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON with numbers as property names

Tags:

json

c#

I have JSON like this:

{"rows":
    {
        "1":{"rowNumber":1,"productID":"100"},
        "2":{"rowNumber":2,"productID":"101"},
        "3":{"rowNumber":3,"productID":"102"}
    }
}

I need to build domain model.

For example:

class Row 
{
    public int rowNumber{get; set;}
    public string productID{get; set;}
}

Root object

class RootObject
{
   public ? ? rows {get; set;}
}

What kind of type have to be rows propperty?

like image 303
dm k Avatar asked Dec 19 '22 08:12

dm k


1 Answers

The answer is

public Dictionary<int, Row> rows { get; set; }

and use

JsonConvert.DeserializeObject<RootObject>(json);

for deserialization. Where JsonConvert is from Newtonsoft library.

like image 97
Ivan Chepikov Avatar answered Dec 24 '22 00:12

Ivan Chepikov