Home c# Deserializing many different JSON in C #

Deserializing many different JSON in C #




There are about a hundred (more in the future) JSON. Each has its own structure JSON. It is necessary to deserialize them all. I am trying to use a standard class DataContractJsonSerializer , use this scheme.

When you create a copy of it needs to pass the class type being serialized or deserialized instances

DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Person));

As I said, I have a lot of different types, but the method of performing deserialization one. I have a string parameters with the class names that describe the structure of the JSON, for example: “Person”, “Address”, etc.


As their substitute in the designer DataContractJsonSerializer so as not to write a hundred cases? Is this possible using standard methods?

I tried to this , but does not work.

How do these problems are solved in an industrial environment?

The Internet is full of examples with a single hard-coded JSON, but about a lot of different nowhere to be found.

Answer 1, Authority 100%

If you initially know which object is represented in json’e, then you only need to use Generic methods, for desserializatsii:

var myNewObject = JsonConvert.DeserializeObject & lt; MyClass & gt; (json);

Answer 2, Authority 83%

You should use JSON.NET (like this connected to the project).

After that to not write type, you can deserialize in JObject :

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
string json = "{\ n" +
       "\" Access_token \ ": \" 13a85707de16fbb1c290250872f30e0b \ ", \ n" +
       "\" Error \ ": \ n" +
       "{\" Code \ ": \" 0000 \ ", \" text \ ": \" ok \ "}, \ n" +
       "\" Duration \ ": 0.025489807128906 \ n" +
var o = JObject.Parse (json);

If you know the name of the property, it is easy to use:

var accessToken = (string) o [ "access_token"]; // "13a85707de16fbb1c290250872f30e0b"
var errorCode = (int) o [ "error"] [ "code"]; // 0.
var duration = TimeSpan.FromSeconds ((double) o [ "duration"]); // {00: 00: 00.0250000}

Answer 3, Authority 50%

If your application is available for assembly, which is the right class, and you have the name of the class, you can download the assembly, to find in it the appropriate type and then pass it to the constructor DataContractJsonSerializer :

using system;
using System.Reflection;
using System.Runtime.Serialization.Json;
AssemblyName asn = new AssemblyName ( "Assembly.Name");
Assembly assembly = Assembly.Load (asn);
Type tp = assembly.GetType ( "Person");
if (tp == null)
  throw new TypeAccessException ();
var ser = new DataContractJsonSerializer (tp);
object person = ser.ReadObject (stream); // = & gt; Person

Answer 4

public static T FromJson & lt; T & gt; (this string objString)
  var stream = objString.GetMemoryStream ();
  stream.Position = 0;
  var ser = new DataContractJsonSerializer (typeof (T));
  return (T) ser.ReadObject (stream);
public static string ToJson & lt; T & gt; (this T obj)
  var stream = new MemoryStream ();
  var ser = new DataContractJsonSerializer (typeof (T));
  ser.WriteObject (stream, obj);
  stream.Position = 0;
  var sr = new StreamReader (stream);
  return sr.ReadToEnd ();

One BUT: does not work with multidimensional arrays.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions