I have a class called DataItem with three properties: Id, DataValue and CreatedDateTime. Properties are defined in the class in this order from top to bottom. This is also the order that I would like to see in my JSON export. The problem is the properties of the DataItem object and in the JSON export, they are sorted alphabetically. Although there is nothing technically wrong with this format, it is a matter of readability. How can I control the order of properties in a JSON export?
I checked the dataItem wen instance and the properties are listed in alphabetical order. This is normal, I understand the potential usability problems without sorting the properties alphabetically.
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 a list of DataItems. I think I need to do something here, but I'm not sure what and how.
public List<DataItem> GetCollection() { return DataRetriever.GetAllDataItems(); }
source share