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;  
       ms.Close();  
       return deserializedUser;  
     }  
   }  
   [DataContract]  
   public class Person  
   {  
     [DataMember]  
     internal string name;  
     [DataMember]  
     internal int age;  
   }  
 }  

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s

%d bloggers like this: