Newtonsoft.Json反序列化 转成Json字符串
发布时间:2020-12-16 19:05:03 所属栏目:百科 来源:网络整理
导读:1:需要引用Newtonsoft.Json.dll 2:命名空间using Newtonsoft.Json DataTable newTable = new DataTable(); newTable.Columns.Add("Hour"); newTable.Columns.Add("PV"); newTable.Columns.Add("IP",typeof(int)); newTable.Columns["PV"].DataType = typeo
1:需要引用Newtonsoft.Json.dll 2:命名空间using Newtonsoft.Json DataTable newTable = new DataTable(); newTable.Columns.Add("Hour"); newTable.Columns.Add("PV"); newTable.Columns.Add("IP",typeof(int)); newTable.Columns["PV"].DataType = typeof(int);//指定类型 newTable.Rows.Add("01:00","2","21"); newTable.Rows.Add("02:00","4","10");
int[] Arr = { 0,1 }; List<string> list = new List<string>() { "PV","IP" }; string[] name = { "今日PV","今日IP" }; var json = new { categories = newTable.Rows.Cast<DataRow>().Select(u => u[0]),series = Arr.Select(u => new { name = name[u],data = newTable.Rows.Cast<DataRow>().Select(row => new { name = row[0],y = row[list[u]] }),}),chartOptions = new { title = new { text = "" },xAxis = new { showFirstLabel = true,showLastLabel = true,tickmarkPlacement = "on" },yAxis = new { title = new { enabled = false },name = "网站数据" },plotOptions = new { series = new { marker = new { enabled = false } } },credits = new { text = "" } } }; string str_ = JsonConvert.SerializeObject(json); 输出结果 { "categories":["02:00","01:00"],"series":[{"name":"今日PV","data":[{"name":"02:00","y":"4"},{"name":"01:00","y":"2"}]},{"name":"今日IP","y":"10"},"y":"21"}]}],"chartOptions":{"title":{"text":""},"xAxis":{"showFirstLabel":true,"showLastLabel":true,"tickmarkPlacement":"on"},"yAxis":{"title":{"enabled":false},"name":"网站数据"},"plotOptions":{"series":{"marker":{"enabled":false}}},"credits":{"text":""}} } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |