json字符串格式的出现,大大地方便了不同系统间的数据传输,无论是Net项目还是Java项目都适用。今天就分享几个json的常用操作。我们是基于Newtonsoft.Json这个第三方类库实现的,使用Nuget就可以直接下载
1、动态创建类
Model类可以是静态创建好的,也可以是动态生成的。使用dynamic这个类就可以,非常方便 。下面2种方法都可以,效果一样
//1,动态创建类 //1.1,方法1 dynamic dynaP1 = new { id = "11", name = "name11" }; //1.2,方法2 dynamic dynaP2 = new ExpandoObject(); dynaP2.id = "11"; dynaP2.name = "name11";
2、Object转Json串
这是最常用的操作了,代码实现也简单
//2,Object生成json串 string json2 = JsonHelper.GetJsonByObject(dynaP1);
3、DataTable转Json串
和Object稍微有些区别,但也能实现
//3,DataTable生成json串 DataTable dt3 = new DataTable(); dt3.Columns.Add("id", typeof(int)); dt3.Columns.Add("name", typeof(string)); dt3.Columns.Add("ctime", typeof(DateTime)); for (int i = 1; i <= 20; i++) { dt3.Rows.Add(new object[] { 1, "name_" + i.ToString(), DateTime.Now }); } string json3 = JsonHelper.GetJsonByDataTable(dt3);
4、json串转List<T>
//4,json串转List<T> List<dynamic> listP4 = new List<dynamic>(); dynamic dynaP41 = new ExpandoObject(); dynaP41.id = 1; dynaP41.name = "name1"; listP4.Add(dynaP41); dynamic dynaP42 = new ExpandoObject(); dynaP42.id = 2; dynaP42.name = "name2"; listP4.Add(dynaP42); string json4 = JsonHelper.GetJsonByObject(listP4); List<PersonModel> list41 = JsonHelper.GetObjectByJson<List<PersonModel>>(json4); int rowCount41 = list41.Count; //结果:2
5、json串转DataTable
//5,json串转DataTable DataTable dt5 = new DataTable(); dt5.Columns.Add("id", typeof(int)); dt5.Columns.Add("name", typeof(string)); dt5.Columns.Add("ctime", typeof(DateTime)); for (int i = 1; i <= 20; i++) { dt5.Rows.Add(new object[] { 1, "name_" + i.ToString(), DateTime.Now }); } string json5 = JsonHelper.GetJsonByDataTable(dt5); DataTable dt51 = JsonHelper.JsonToDataTable(json5);
6、Xml和Json互转
这个场景在开放平台的API参数里,Json没出现之前,都是用Xml传输数据的
//6,json和xml格式互转 List<PersonModel> list6 = new List<PersonModel>(); for (int i = 1; i < 10; i++) { list6.Add(new PersonModel() { id = i, name = "name-" + i.ToString(), ctime = DateTime.Now, }); } //上面是真正的json串数据,下面要额外加工下 dynamic dyna6Root = new ExpandoObject(); dyna6Root.json1 = list6; string json6Root = JsonHelper.GetJsonByObject(dyna6Root); //json转xml string json6ToXml = @"{ ""?xml"":{ ""@version"": ""1.0"", ""@standalone"": ""no"" },""root"":" + json6Root + "}"; string xml6 = JsonHelper.JsonToXml(json6ToXml); //Xml转Json string json6ByXml = JsonHelper.XmlToJson(xml6);
7、转为微软的Json类
//7,转为微软json类 PersonModel model7 = new PersonModel() { id = 111, name = "name-111", ctime = DateTime.Now, }; string json7 = JsonHelper.GetJsonByObject(model7); Dictionary<string, object> dic7 = JsonHelper.GetObjectByJsonMS(json7) as Dictionary<string, object>; object objId = dic7["id"]; //结果:111
8、某些属性特殊化
比如属性名和类中命名的不同、某些属性不需要显示等
//8,某些属性名称特殊化 PersonModel model8 = new PersonModel() { id = 15, name = "name-15", ctime = DateTime.Now, alias_name = "alias-name-15", sex = "1" }; //{"id":15,"name":"name-15","ctime":"2022-10-07 19:31:52","AliasName":"alias-name-15"} string json8 = JsonHelper.GetJsonByObject(model8);
public class PersonModel { public int id { get; set; } public string name { get; set; } public DateTime ctime { get; set; } /// <summary> /// 生成的json串中,其属性名是AliasName /// </summary> [JsonProperty(PropertyName = "AliasName")] public string alias_name { get; set; } /// <summary> /// 此特性表示不出现在json串中 /// </summary> [JsonIgnore] public string sex { get; set; } }
相关阅读
文章评论