-
引用DLL
文件:Newtonsoft.Json.dll 路径:D:\yonyou\U9CE\Portal\bin\
-
C#实体类
List<Departments> deptss = new List<Departments>(); private void Department_Load(object sender, EventArgs e) { strUri = TexURI.Text; strToken = GetToken(TexURI.Text); //Department赋值 deptss.Add(new Departments { OrgCode = "501", DeptCode = "0101", DeptName = "生产部" }); deptss.Add(new Departments { OrgCode = "501", DeptCode = "0102", DeptName = "制造一课" }); deptss.Add(new Departments { OrgCode = "501", DeptCode = "0103", DeptName = "制造二课" }); deptss.Add(new Departments { OrgCode = "501", DeptCode = "0104", DeptName = "制造三课" }); deptss.Add(new Departments { OrgCode = "501", DeptCode = "0105", DeptName = "制造四课" }); } /// <summary> ///部门结构定义 /// </summary> public class Departments { /// <summary> /// 组织编码 /// </summary> public string OrgCode { get; set; } /// <summary> /// 部门编码 /// </summary> public string DeptCode { get; set; } /// <summary> /// 部门名称 /// </summary> public string DeptName { get; set; } }
-
C#序列化
C#实体数据序列化Json串;
/// <summary> /// 序列化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnJsonX_Click(object sender, EventArgs e) { TexResult.Text = JsonConvert.SerializeObject(deptss);//SerializeObject序列化对象 }
-
C#反序列化
根据序列化Json串结果,反序列化Json取值;
/// <summary> /// 反序列化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnJsonF_Click(object sender, EventArgs e) { List<Departments> Enmity = JsonConvert.DeserializeObject<List<Departments>>(TexResult.Text);//DeserializeObject 反序列化对象 string strresult = ""; foreach (Departments line in Enmity) { strresult += line.OrgCode + " " + line.DeptCode + " " + line.DeptName + "\r\n"; } TexResult.Text = strresult; }标签:Departments,deptss,DeptCode,OrgCode,序列化,DeptName From: https://www.cnblogs.com/youmin3205/p/18204019