首页 > 代码库 > json字符串反序列化成匿名类型对象

json字符串反序列化成匿名类型对象

添加第三方引用

using Newtonsoft.Json;

/// <summary>
/// 匿名类型解析
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <param name="anonymousTypeObject"></param>
/// <returns></returns>
public static T DeserializeAnonymousType<T>(string jsonString, T anonymousTypeObject)
{
return JsonConvert.DeserializeAnonymousType(jsonString, anonymousTypeObject);
}

 

调用方,无需专门定义一个类

string jsonString="";

var definition = new { name= "", sex= "", age= "" };
var encryptObject = JsonHelper.DeserializeAnonymousType(jsonString, definition);

// 获取数据
string name= encryptObject.name;
string sex= encryptObject.sex;
string age= encryptObject.age;

 

json字符串反序列化成匿名类型对象