dm k
dm k

Reputation: 131

Deserialize JSON with numbers as property names

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?

Upvotes: 5

Views: 5687

Answers (3)

Ivan  Chepikov
Ivan Chepikov

Reputation: 825

The answer is

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

and use

JsonConvert.DeserializeObject<RootObject>(json);

for deserialization. Where JsonConvert is from Newtonsoft library.

Upvotes: 14

Jonathan Burgos G.
Jonathan Burgos G.

Reputation: 344

your question is not clear,But this can be useful

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
};
string json = @"{
   'Email': '[email protected]',
   'Active': true,
   'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
     'User',
     'Admin'
   ]
 }";

Account account = JsonConvert.DeserializeObject<Account>(json);

Console.WriteLine(account.Email);

Upvotes: 0

Mitchell Urgero
Mitchell Urgero

Reputation: 43

Seems like you are looking for either int type or int32 type:

//Assumes you are using Newtonsoft JSON Nuget Package

List<int> products = null;

products = JsonConvert.DeserializeObject<List<int>>(json);

Should work - but all code you find on StackOverflow should be looked at as pseudo code. this should at least point you in the right direction. Hope it helped!

Upvotes: 0

Related Questions