C# JsonSerialize 没有成员名称

发布于 2024-11-08 00:09:26 字数 1825 浏览 1 评论 0原文

我有以下类,我喜欢将其序列化为 Json。我目前得到这样的东西

 var pgdata =   {"SeriesList":[{"label":"Fund","data":[{"Year":2000,"RateOfReturn":0.02},{"Year":2009,"RateOfReturn":0.03}]},{"label":"Benchmark","data":[{"Year":2000,"RateOfReturn":0.024},{"Year":2009,"RateOfReturn":0.032}]}]} ;

我喜欢得到这样的

var pgdata =   {"SeriesList":[{"label":"Fund","data":[[2000,0.02],[2009,0.03]]},{"label":"Benchmark","data":[[2000,0.024],[2009,0.032]]}]} ;

C# 代码:

public class JsonPerformGraphData
    {
        public List<Series> SeriesList = new List<Series>();

        public Series GetSeries(string name)
        {
            foreach (Series s in SeriesList)
            {
                if (s.label == name)
                    return s;
            }

            Series series = new Series(name);
            SeriesList.Add(series);
            return series;
        }

        public void Add(PerformanceGraphItem ppgi)
        {
            Series s = GetSeries(ppgi.PerformanceGraphSeries.Name);
            s.addItem(ppgi.Year, ppgi.RateOfReturn);
        }


        public class Series
        {
            public string label;
            public List<Data> data = new List<Data>();

            public Series(String name)
            {
                label = name;
            }

            public void addItem(short year, double rateOfReturn)
            {

                data.Add(new Data(year,rateOfReturn));

            }
        }

        public class Data
        {
            public short Year;
            public double RateOfReturn;

            public Data(short year, double rateOfReturn)
            {
                Year = year;
                RateOfReturn = rateOfReturn;
            }
        }

    }

I have the following classes that I like to serialize to Json. I'm currently getting something like this

 var pgdata =   {"SeriesList":[{"label":"Fund","data":[{"Year":2000,"RateOfReturn":0.02},{"Year":2009,"RateOfReturn":0.03}]},{"label":"Benchmark","data":[{"Year":2000,"RateOfReturn":0.024},{"Year":2009,"RateOfReturn":0.032}]}]} ;

I like to get something like this

var pgdata =   {"SeriesList":[{"label":"Fund","data":[[2000,0.02],[2009,0.03]]},{"label":"Benchmark","data":[[2000,0.024],[2009,0.032]]}]} ;

C# Code:

public class JsonPerformGraphData
    {
        public List<Series> SeriesList = new List<Series>();

        public Series GetSeries(string name)
        {
            foreach (Series s in SeriesList)
            {
                if (s.label == name)
                    return s;
            }

            Series series = new Series(name);
            SeriesList.Add(series);
            return series;
        }

        public void Add(PerformanceGraphItem ppgi)
        {
            Series s = GetSeries(ppgi.PerformanceGraphSeries.Name);
            s.addItem(ppgi.Year, ppgi.RateOfReturn);
        }


        public class Series
        {
            public string label;
            public List<Data> data = new List<Data>();

            public Series(String name)
            {
                label = name;
            }

            public void addItem(short year, double rateOfReturn)
            {

                data.Add(new Data(year,rateOfReturn));

            }
        }

        public class Data
        {
            public short Year;
            public double RateOfReturn;

            public Data(short year, double rateOfReturn)
            {
                Year = year;
                RateOfReturn = rateOfReturn;
            }
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-11-15 00:09:26

您不使用 json.NET 是否有特殊原因?它已经为 C# 开发人员解决了序列化问题,并且没有什么理由不使用它。我遇到的唯一问题与它在 JSON 反序列化中使用接口对象的不灵活性有关,但除非您正在对此类进行单元测试,否则这不应该是一个问题(而且看起来您也不是这样)。

出于您的目的,json.NET 应该为您提供您正在寻找的输出,而无需自行滚动序列化器。

编辑:另外,需要明确的是,json.NET 确实支持按您想要的方式进行没有成员名称的序列化。只需向每个成员添加 JsonProperty 属性 即可。

Is there a particular reason you're not using json.NET? It has already solved the serialization problem for C# developers and there's fairly few reasons not to use it. The only problem I had with it related to its inflexibility with using interface objects in deserialization from JSON, but unless you are doing unit testing on this class that shouldn't be an issue (and it doesn't look like you are).

For your purposes, json.NET should give you the output you're looking for, with no muss or fuss on rolling a serializer all by yourself.

edit: Also, just to be clear, json.NET does support serializing without member names the way you're wanting. Simply add a JsonProperty Attribute to each member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文