2

I have a List<CampaignModel> that I need to serialize using JsonConvert.SerializeObject

Everything works fine except Im getting the properties in a diferent order than the class declaration.

My class declaration is:

 public class CampaignModel
    {
        public string Checked { get; set; }
        public int CampaignId { get; set; }
        public string Name { get; set; }
        public string Market { get; set; }
        public string Type { get; set; }
        public bool IsActive { get; set; }
        public bool Active { get; set; }
    }

And the order Im getting the properties in my json is:

enter image description here

Any clue?

VAAA
  • 14,531
  • 28
  • 130
  • 253
  • 1
    According to [JSON standard](http://json.org/), *An object is an **unordered** set of name/value pairs.* So property order should not matter. That being said, if your receiving system cannot handle properties in any order, you can use the [`[JsonProperty(Order=XXX)]`](http://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm) attribute to declare your desired order. – dbc Oct 02 '15 at 21:17
  • 1
    JSON is a textual format. What your screenshot shows is a visualization of an object (that was probably created by parsing a JSON string). – Amit Oct 02 '15 at 21:22
  • I think you are getting tricked by your debugger, which is sorting the keys by name. Json.Net seems to honor the order of the declared properties by default. Fiddle: https://dotnetfiddle.net/NJkOuA. Regardless, what @dbc said is correct; it really should not matter to your code what order the properties appear in. – Brian Rogers Oct 02 '15 at 21:30
  • Does this answer your question? [C# Get FieldInfos/PropertyInfos in the original order?](https://stackoverflow.com/questions/5473455/c-sharp-get-fieldinfos-propertyinfos-in-the-original-order) – Ian Kemp Jun 03 '20 at 15:26

3 Answers3

2

You can use the JsonProperty attribute

[JsonProperty(Order = 1)]

Documentation: JsonPropertyAttribute order

So assuming the order you want is the order in your code, you would have something like this:

public class CampaignModel
{
    [JsonProperty(Order = 1)]
    public string Checked { get; set; }
    [JsonProperty(Order = 2)]
    public int CampaignId { get; set; }
    [JsonProperty(Order = 3)]
    public string Name { get; set; }
    [JsonProperty(Order = 4)]
    public string Market { get; set; }
    [JsonProperty(Order = 5)]
    public string Type { get; set; }
    [JsonProperty(Order = 6)]
    public bool IsActive { get; set; }
    [JsonProperty(Order = 7)]
    public bool Active { get; set; }
}

The reason for setting the order on all properties is because any without will be given the default of -1. Which will place them before any ordered properties.

A .Net Fiddle (forked from Brian Rogers) showing a quick example of the attributes use - https://dotnetfiddle.net/wr2KRh

Stuart
  • 754
  • 11
  • 25
1

Properties are not guaranteed to have or maintain any specific order in JavaScript. What you will do in your C# code can't change this "limitation".

BTW, the same goes for .net.

Community
  • 1
  • 1
Amit
  • 45,440
  • 9
  • 78
  • 110
1

You can specify the order for the members of a class you wish to serialize. The basic rules for data ordering include:

  • If a data contract type is a part of an inheritance hierarchy, data members of its base types are always first in the order
  • Next in order are the current type’s data members that do not have the Order property of the DataMemberAttribute attribute set, in alphabetical order
  • Next are any data members that have the Order property of the DataMemberAttribute attribute set. These are ordered by the value of the Order property first and then alphabetically if there is more than one member of a certain Order value. Order values may be skipped.

So you may specify the order of the members of the class with the Order property:

[JsonProperty(Order = 1)]

More about JSON Serialization on MSDN

S.Dav
  • 2,436
  • 16
  • 22