Deserialize JSON Data using DataContractJsonSerializer

In one of the integration, I needed to de-serialize a json to an object inside the AX.

To de-serialize we have used DataContractJsonSerializer and created a .NET class to pass the JSON parameter (string) and return is an object 🙂 .. Success! see below!

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.IO;  
 using System.Runtime.Serialization.Json;  
 using System.Runtime.Serialization;  
 namespace Integration.JsonAdapter  
   public class Convert  
     // Deserialize a JSON stream to a User object.   
     public static Person ReadToObject(string json)  
       Person deserializedUser = new Person();  
       MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));  
       DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());  
       deserializedUser = ser.ReadObject(ms) as Person;  
       return deserializedUser;  
   public class Person  
     internal string name;  
     internal int age;  

Now we can add the dll to AX and call the method to do the de-serialization to Person Object.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: