如何控制我的JSON输出属性的顺序? [英] How do I control the order of properties in my JSON output?

查看:551
本文介绍了如何控制我的JSON输出属性的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,名为DataItem的,具有三个属性:ID,DataValue和CreatedDateTime。的属性是在该类中定义的顺序从上到下。这也是为了我想看看我的JSON出口的属性。问题是在DataItem的对象和JSON出口属性按字母顺序排序。虽然技术上没有什么不对的格式,它是readability.How的事我控制属性的顺序在JSON出口?

我查了DataItem的文实例化和属性按字母顺序列出。这是正常的,我不明白的属性进行排序按字母顺序排列。

潜在的可用性问题

 公共静态列表< D​​ataItem的> GetAllDataItems()
        {
            名单< D​​ataItem的> dataItems =新的名单,其中,DataItem的>();

            SqlConnection的康恩= NetduinoDb.GetConnection();

            的SqlCommand CMD = conn.CreateCommand();

            cmd.CommandText =选择编号,DataValue,CreatedDateTime来自XXX;
            cmd.CommandType = CommandType.Text;

            conn.Open();

            SqlDataReader的读者= cmd.ExecuteReader();

            而(reader.Read())
            {
                的DataItem DataItem的=新的DataItem
                {
                    编号=读者[ID]的ToString()
                    DataValue =读卡器[DataValue。toString()方法,
                    CreatedDateTime =读卡器[CreatedDateTime。的ToString()
                };

                dataItems.Add(DataItem的);
            }

            reader.Close();
            conn.Close();


            返回dataItems.ToList();
        }
 

这方法在我的服务实现和返回DataItems名单。我想,我需要做的东西在这里,但不知道怎么去的。

 公开名单< D​​ataItem的> GetCollection()
{
    返回DataRetriever.GetAllDataItems();
}
 

解决方案

DataContractJsonSerializer 考虑到了数据成员属性里面有一个订单属性。你可以用它来告诉串行想要序列化的成员的顺序。

  [DataContract]
一流的DataItem
{
    [数据成员(ORDER = 1)]
    公共字符串ID {获得;组; }

    [数据成员(顺序= 2)]
    公共字符串DataValue {获得;组; }

    [数据成员(顺序= 3)]
    公共字符串CreatedDateTime {获得;组; }
}
 

根据需要

调整顺序,但是这是通常它是如何在WCF进行。

I have a class, named DataItem, with three properties: Id, DataValue, and CreatedDateTime. The properties are defined in the class in that order from top to bottom. This is also the order I'd like to see the properties in my JSON export. The problem is the properties in the DataItem object and in the JSON export are sorted in alphabetical order. Although there is nothing technically wrong with this format, it is a matter of readability.How do I control the order of properties in the JSON export?

I checked the dataItem wen instantiated and the properties are listed in alphabetical order. This is okay, I understand the potential usability issues of not sorting properties alphabetical.

public static List<DataItem>GetAllDataItems()
        {
            List<DataItem> dataItems = new List<DataItem>();

            SqlConnection conn = NetduinoDb.GetConnection();

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "Select Id, DataValue, CreatedDateTime from XXX";
            cmd.CommandType = CommandType.Text;

            conn.Open();

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataItem dataItem = new DataItem
                {
                    Id = reader["Id"].ToString(),
                    DataValue = reader["DataValue"].ToString(),
                    CreatedDateTime = reader["CreatedDateTime"].ToString()
                };

                dataItems.Add(dataItem);
            }

            reader.Close();
            conn.Close();


            return dataItems.ToList();
        }

This method is in my service implementation and returns the list of DataItems. I'm thinking I need to do something here but not sure what or how.

public List<DataItem> GetCollection()
{
    return DataRetriever.GetAllDataItems();
}

解决方案

The DataContractJsonSerializer takes into consideration the DataMember attribute which has an Order property. You can use it to tell the serializer the order of the members you want serialized.

[DataContract]
class DataItem
{
    [DataMember(Order = 1)]
    public string Id { get; set; }

    [DataMember(Order = 2)]
    public string DataValue { get; set; }

    [DataMember(Order = 3)]
    public string CreatedDateTime { get; set; }
}

Adjust the order as needed, but this is generally how it is done in WCF.

这篇关于如何控制我的JSON输出属性的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆