Is there a way to deserialize JSON content into a C# 4 dynamic type? It would be nice to skip creating a bunch of classes in order to use the DataContractJsonSerializer.
相关问题
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- Graphics.DrawImage() - Throws out of memory except
- StackExchange API - Deserialize Date in JSON Respo
I use: http://json2csharp.com/ to get A class representing the Json Object.
Input:
Output:
After that I use Newtonsoft.Json to fill the Class:
You can call it like that:
PS:
If your json variable name is no valid C# name (name starts with
$
) you can fix that like this:.Net 4.0 has a built-in library to do this:
This is the simplest way.
For that I would use JSON.NET to do the low-level parsing of the JSON stream and then build up the object hierarchy out of instances of the
ExpandoObject
class.use DataSet(C#) With javascript simple function for create json stream with DataSet input create json like(multi table dataset) [[{a:1,b:2,c:3},{a:3,b:5,c:6}],[{a:23,b:45,c:35},{a:58,b:59,c:45}]]
just client side use eval for example
var d=eval('[[{a:1,b:2,c:3},{a:3,b:5,c:6}],[{a:23,b:45,c:35},{a:58,b:59,c:45}]]')
then use
d[0][0].a //out 1 from table 0 row 0
d[1][1].b //out 59 from table 1 row 1
Deserializing in JSON.NET can be dynamic using the
JObject
class, which is included in that library. My JSON string represents these classes:Now we deserialize the string WITHOUT referencing the above classes:
Or if you want to go deeper:
See post for a complete example.
To get an ExpandoObject: