5

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?

dm k
  • 111
  • 3
  • 9
  • 2
    What do you mean by 'with digits as type'? your question is not clear – yonisha Apr 11 '17 at 16:49
  • Make your `"rows"` property be a `Dictionary`. See [Create a strongly typed c# object from json object with ID as the name](http://stackoverflow.com/q/34213566/3744182) and [How can I parse a JSON string that would cause illegal C# identifiers?](http://stackoverflow.com/a/24536564/3744182). – dbc Apr 11 '17 at 17:24
  • -> dbc: Place your comment as answear, I'll mark as correct answear – dm k Apr 12 '17 at 11:20

3 Answers3

12

The answer is

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

and use

JsonConvert.DeserializeObject<RootObject>(json);

for deserialization. Where JsonConvert is from Newtonsoft library.

Ivan Chepikov
  • 795
  • 4
  • 22
0

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!

0

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': 'james@example.com',
   'Active': true,
   'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
     'User',
     'Admin'
   ]
 }";

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

Console.WriteLine(account.Email);