JSON到C#动态列表<object>或DataTable

时间:2016-07-15 02:20:50

标签: c# json

简短版本:如何将JSON字符串反序列化为C#列表或DataTable,而无需将已定义的类反序列化为?

更多解释:我的控制器需要一个json字符串,它是一个对象数组,但该对象的属性是未知的。我需要将其反序列化为一个列表并循环其内容以进行保存。示例json字符串:
1。

[
    {"id":"10","name":"User","add":false,"edit":true,"authorize":true,"view":true},
    {"id":"11","name":"Group","add":true,"edit":false,"authorize":false,"view":true},
    {"id":"12","name":"Permission","add":true,"edit":true,"authorize":true,"view":true}
]

2

[
    {"id":"10","name":"User"},
    {"id":"11","name":"Group"},
    {"id":"12","name":"Permission"}
]

2 个答案:

答案 0 :(得分:3)

initialized multiple times

答案 1 :(得分:0)

反序列化您的Json并将其直接转换为数据表。

DataTable dt = (DataTable)JsonConvert.DeserializeObject(jsonText, (typeof(DataTable)));

请参阅此答案:https://stackoverflow.com/a/27282579/4827151