您的位置:首页 - 教程 - C# - 正文
C# Json数据转数组 泛型方法
Json数据格式:

var jsontext = "[{"id":"815bb899-8d70-4745-b799-7e68840a2b34","text":"设计","pid":"57eb2d57-9384-4d9f-a399-2c228fea81c2"},{"id":"4c76b72c-35da-4749-893c-5c7dde9431e6","text":"市政工程","pid":""}]";



/// <summary>
/// Json数据转数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonText"></param>
/// <returns></returns>
public List<T> JsonToList<T>(string jsonText)
{
List<T> list = new List<T>();

DataContractJsonSerializer _Json = new DataContractJsonSerializer(list.GetType());
byte[] _Using = System.Text.Encoding.UTF8.GetBytes(jsonText);
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
_MemoryStream.Position = 0;

return (List<T>)_Json.ReadObject(_MemoryStream);
}





public class CheckedTreeInfo
{
/// <summary>
/// 父节点Id
/// </summary>
public string pid { get; set; }
/// <summary>
/// 节点Id
/// </summary>
public string id { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string text { get; set; }
}

评论: