super-user super-user - 4 months ago 54
JSON Question

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"}
]

Answer
dynamic jsonObject = System.Web.Helpers.Json.Decode(jsonText);
Comments