Skip to main content

Notifications

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.


This was originally posted here.

Comments

*This post is locked for comments