最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

使用DataContractJsonSerializer反序列化JSON数据 / Deserialize JSON Data using DataContractJsonSerializer

网络文摘 William 1719浏览 0评论

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.

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址