如何在没有某些属性名称的情况下序列化对象

时间:2017-01-14 09:11:06

标签: c# asp.net json serialization

我想生成以下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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

请在此处使用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; }
    }

答案 1 :(得分:0)

如果您希望在序列化之后获得Json输出,例如在您的问题中发布。您应该遵循此类层次结构:

public class Sample
{
    [JsonProperty("data")]
    public List<List<double>> Data { get; set; }

    [JsonProperty("label")]
    public string Label { get; set; }
}

然后你应该创建应该通过Json.Net

序列化的样本数组
var samples = new List<Sample>();
samples.Add(new Sample()
{
    Data = new List<List<double>>()
    {
        new List<double> { 0, 6.48 },
        new List<double> { 1, 6.43 }
        //...other values
    },
    Label = "Port-1"
};
string json = JsonConvert.SerializeObject(samples);