如何控制我的JSON输出属性的顺序?我的、顺序、属性、JSON

2023-09-07 15:50:20 作者:手踹兜兜笑看情侣分手

我有一个类,名为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 {获得;组; }
}
 
js中的json对象的属性怎么取值

根据需要

调整顺序,但是这是通常它是如何在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.