Newtonsoft.Json反序列化 转成Json字符串

前端之家收集整理的这篇文章主要介绍了Newtonsoft.Json反序列化 转成Json字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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":""}}
}
原文链接:https://www.f2er.com/json/289666.html

猜你在找的Json相关文章