There are (hacker) ways to do this ... but you shouldn't.
In JSON, an object is defined this way:
An object is an unordered set of name / value pairs.
See http://json.org .
Most JSON implementations take no effort to preserve the order of the name / value pairs of an object, since it (by definition) is not significant.
If you want the order to be saved, you need to redefine the data structure; eg.
{ "items": [ [ {"WR":"qwe"}, {"QU":"asd"}, {"QA":"end"}, {"WO":"hasd"}, {"NO":"qwer"} ], ] }
or more simply:
{ "items": [ {"WR":"qwe"}, {"QU":"asd"}, {"QA":"end"}, {"WO":"hasd"}, {"NO":"qwer"} ] }
Followup
Thanks for the info, but I have no choice but to use JSON in my application, and my application needs to keep the key order regardless of the definition of the JSON object ... I am not allowed to change the format of the JSON file, as well ...
You need to have a tough conversation with the person who designed this file structure and will not let you change it. It is not right. You need to convince them.
If they really won't let you change it:
- You must insist not to call it JSON ... 'Because it is not.
- You must indicate that you will have to write / modify the code specifically to handle this non-JSON format ... if you cannot find some JSON implementation that saves the order. If they are a paid customer, make sure they pay for this extra work that you have to do.
- You must indicate that if "not JSON" should be used by some other tool, this will be problematic. Indeed, this problem will occur again and again ...
Such a thing as very bad. On the one hand, your software will violate a well-established / long-standing specification designed to ensure interoperability. On the other hand, nit-wits that developed this chromatic (not JSON!) File format are likely to be removed from other people's systems, etc., because systems cannot handle their stupidity.
UPDATE
Also worth reading what says