简体   繁体   中英

JSON to C# a Dynamic List<object> or DataTable

Short version: How can I deserialize a JSON string into a C# list or DataTable without having a defined class to deserialize to?

More explanation: My controller expects a json string which is an array of objects but the properties of the object is unknown. I need to deserialize it into a list and loop through its contents for saving. Sample json strings:
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"}
]
dynamic jsonObject = System.Web.Helpers.Json.Decode(jsonText);

Deserialize your Json and cast it directly to datatable.

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

Refer to this answer : https://stackoverflow.com/a/27282579/4827151

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM