Reputation: 1057
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"}
]
Upvotes: 0
Views: 5361
Reputation: 2978
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
Upvotes: 0