Properties Order while Deserialization using JSON.Net

I am trying to Deserialize the string to JSON Object using JsonConvert.DeserializeObject like below:

 var str = "{ Value: \"File\",Text: \"OWENS & MINOR  INFANT - 2228548\"}";
agreementnodes = JsonConvert.DeserializeObject<List<AgreementNode>>("[" + str + "]");

When the json is converted to an array the properties are alphabetically ordered. Example : Even though Value is first and Text is in the string the properties are displayed like below:

enter image description here

Even in the class declaration, I Value is Firs and Text is second. But when de-serializing, the properties are sorted alphabetically.

[JsonProperty(Order = 9)]
public string Value { get; set; }

[JsonProperty(Order = 10)]
public string Text { get; set; }

Is there any way to retain the order of the properties in the resultant array like I want resultantarray[0] = Value and resultantarray1 = Text?

Upvotes: 1

Views: 2362

Answers (2)

Anders Rundgren
Anders Rundgren

Reputation: 21

The ordering of properties are undefined according to the specification. In spite of that the most current ECMAScript (JavaScript) specification requires an ordered format which also enables new cool applications like: https://cyberphone.github.io/openkeystore/resources/docs/jcs.html#ECMAScript_Compatibility_Mode

Upvotes: 1

Anıl Okay
Anıl Okay

Reputation: 111

Nothing wrong with json.net serializer. It's automatically ordered by alphabetical in quickwatch and watch windows. Check System.Web.HttpContext.SystemWebAssembly if you want. It's not a deserialized code.

enter image description here

Upvotes: 1

Related Questions