[c#]怎么可以没有一些属性名字对象将序列化

标签: Asp.net json C#
发布时间: 2017/2/25 3:31:51
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要生成下面的 json 输出。我试图序列化对象,但从你看到输出没有 json 字符串名称为"数据"数组,如果它序列化会将属性名称添加到数组。请为此提出一个解决方案,优化在泛型方法。这是虚拟数据原始数据将是动态。

[
    {
        "data": [[0, -74984809.4603082], [1, -1547043466.02543], [2, 1197200673.15602], [3, -1329017076.87315]],
        "label": "Sine Wave"
    },
    {
        "data": [[0, -16009669.0446502], [1, -1349266386.03401], [2, 1730901576.10012], [3, -48303271.446511]],
        "label": "Cosine Wave"
    },
    {
        "data": [[0, -22114801.1769572], [1, 1737206285.30856], [2, 1280894300.93367], [3, -14969166.212896]],
        "label": "Sine2 Wave"
    },
    {
        "data": [[0, -236097551.627018], [1, 8971833.78377865], [2, -805237017.202245], [3, -28865332.2392996]],
        "label": "Cosine2 Wave"
    }
]

我的代码︰

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using Microsoft.Ajax.Utilities;

namespace D3WebApp
{
    public partial class _Default : Page
    {
        public class Datum
        {
            public int No { get; set; }

            public double Value { get; set; }
        }
        public class LineGraphData
        {
            public Datum Data { get; set; }

            public string Label { get; set; }
        }

        public class GraphData
        {
            public Datum[] Data { get; set; }

            public string Label { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string GetData()
        {
            return GetDataForChart(); ;
        }

        private static string GetDataForChart()
        {
            var r1 = new Random();
            var r2 = new Random();
            var r3 = new Random();
            var r4 = new Random();
            var datalist = new List<LineGraphData>();

            for (var i = 0; i < 4; i++)
            {

                datalist.Add(new LineGraphData()
                {
                    Data = new Datum()
                    {
                        No = i,
                        Value = r1.Next() * Math.Sin(r2.Next() + i / (10 * (r4.Next() + .5)))
                    },
                    Label = "Sine Wave"
                });

                datalist.Add(new LineGraphData()
                {
                    Data = new Datum()
                    {
                        No = i,
                        Value = r2.Next() * Math.Sin(r3.Next() + i / (10 * (r3.Next() + .5)))
                    },
                    Label = "Cosine Wave"
                });
                datalist.Add(new LineGraphData()
                {
                    Data = new Datum()
                    {
                        No = i,
                        Value = r3.Next() * Math.Sin(r1.Next() + i / (10 * (r2.Next() + .5)))
                    },
                    Label = "Sine2 Wave"
                });

                datalist.Add(new LineGraphData()
                {
                    Data = new Datum()
                    {
                        No = i,
                        Value = r4.Next() * Math.Sin(r4.Next() + i / (10 * (r1.Next() + .5)))
                    },
                    Label = "Cosine2 Wave"
                });
            }

            var sb = new StringBuilder();
            var valuelist = new List<GraphData>();
            //want to remove this or make it cleaner
            foreach (var result in datalist.DistinctBy(x => x.Label).Select(x => x.Label))
            {
                var childItems = datalist.
                    Where(x => x.Label == result).Select(x => x.Data).ToList();

                valuelist.Add(new GraphData() { Label = result, Data = childItems.ToArray() });

                sb.Append(@"{""data"":");
                sb.Append("[");

                foreach (var value in childItems)
                {
                    sb.Append(string.Format("[{0},{1}],", value.No, value.Value));
                }

                var index = sb.ToString().LastIndexOf(',');

                if (index >= 0)
                    sb.Remove(index, 1);

                sb.Append("],");
                sb.Append(string.Format(@"""{0}"":""{1}""", "label", result));
                sb.Append("},");
            }
            var testdata = new JavaScriptSerializer().Serialize(valuelist);
            var datastr = "[" + sb.ToString().Trim(',') + "]";
            return datastr;
        }
    }
}

解决方法 1:

请在这里使用Newtonsoft.JSON和 Linq 方法 GroupBy:

    var datalistDynamic = datalist.GroupBy(it => it.Label).Select(
        dataItemGrouped => new
        {
            Label = dataItemGrouped.Key,
            Data = dataItemGrouped.Select(dataItem => new[] {dataItem.Data.No, dataItem.Data.Value})
        }).ToArray();
    var datastr = JsonConvert.SerializeObject(datalistDynamic);
    return datastr;

可以删除此代码︰

   var sb = new StringBuilder();
            var valuelist = new List<GraphData>();
            //want to remove this or make it cleaner
            foreach (var result in datalist.DistinctBy(x => x.Label).Select(x => x.Label))
            {
                var childItems = datalist.
                    Where(x => x.Label == result).Select(x => x.Data).ToList();

                valuelist.Add(new GraphData() { Label = result, Data = childItems.ToArray() });

                sb.Append(@"{""data"":");
                sb.Append("[");

                foreach (var value in childItems)
                {
                    sb.Append(string.Format("[{0},{1}],", value.No, value.Value));
                }

                var index = sb.ToString().LastIndexOf(',');

                if (index >= 0)
                    sb.Remove(index, 1);

                sb.Append("],");
                sb.Append(string.Format(@"""{0}"":""{1}""", "label", result));
                sb.Append("},");
            }
            var testdata = new JavaScriptSerializer().Serialize(valuelist);
            var datastr = "[" + sb.ToString().Trim(',') + "]";

如果您想要忽略属性,请使用属性 JsonIgnore ,例如

  public class GraphData
    {
        public Datum[] Data { get; set; }

        [JsonIgnore]
        public string Label { get; set; }
    }
赞助商