2017-04-11 170 views
1

我有JSON是这样的:反序列化JSON以数字作为属性名

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

我需要建立域模型。

例如:

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

根对象

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

有什么样的类型是行propperty?

+2

你是什么意思'与数字作为类型'?你的问题不清楚 – yonisha

+0

使你的''行''属性成为'字典'。请参阅[使用ID作为名称从json对象创建一个强类型的c#对象](http://stackoverflow.com/q/34213566/3744182)和[如何解析会导致非法C#标识符的JSON字符串?]( http://stackoverflow.com/a/24536564/3744182)。 – dbc

+0

- > dbc:把你的评论作为answear,我会标记为正确的answear –

回答

2

答案是

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

,并使用

JsonConvert.DeserializeObject<RootObject>(json); 

反序列化。 JsonConvert来自Newtonsoft库。

0

好像你正在寻找的是int型或INT32类型:

//Assumes you are using Newtonsoft JSON Nuget Package 

List<int> products = null; 

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

应工作 - 但你在计算器上找到所有的代码应该被看作是伪代码。这至少应该让你指向正确的方向。希望它有帮助!

0

你的问题不太清楚,但这可能是有用的

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);