C#中反序列化json实例
1、引入核心
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
2、核心
public static Result JsonDeSerializer(string Json)
{
if (Json != null)
{
//反序列化
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(Json)))
{
DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(Result));
Result model = (Result)deseralizer.ReadObject(ms);// //反序列化ReadObject
return model;
}
}
return null;
}
3、实体类
public class Result
{
[DataMember]
public string msg { get; set; }
[DataMember]
public string code { get; set; }
[DataMember]
public Data data { get; set; }
}
public class Data
{
[DataMember]
public double money { get; set; }
}