Decode a JSON object in C # without prior knowledge of type

I have a working code:

String objstr = "{\"m_children\":[{\"m_children\":null,\"m_name\":\"child0\"},{\"m_children\":null,\"m_name\":\"child1\"}],\"m_name\":\"Root\"}"; byte[] byteArr = Encoding.ASCII.GetBytes(objstr); MemoryStream ms = new MemoryStream(byteArr); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Node)); Node obj = (Node)ser.ReadObject(ms); 

What bothers me is that I need to know the type of object contained in the string before decrypting it. I wanted to send an object encoded in JSON through a TCP channel, and should not send additional information about what type of object it has.

+6
source share
2 answers

With .NET 4.0 you can use dynamic objects. Why not try this solution from another question: Deserialize JSON to a dynamic C # object?

+1
source

Additional Information:

http://www.codeproject.com/KB/IP/fastJSON.aspx

When this library encodes JSON, it provides enough information for fully automatic reuse.

This is not suitable for my purpose, but although it includes a bunch of C # -special information, we work in different languages ​​here.

0
source

Source: https://habr.com/ru/post/895746/


All Articles