This is a weird issue. I understand JSON is supposed to be unordered and the implementation of java.lang.JSONObject specifically mentions that it is unordered.
However, it puzzles me because I would expect the way I call the put(); methods it should keep my order. Maybe that contradicts what I just said in the beginning though.
I am using JSON to communicate with a WCF web service which requires me to include a __type key to designate the type of complex object I am sending.
I urge you to try this:
JSONObject object = new JSONObject();
try {
object.put("__type", "Value1");
object.put("SecondKey", "Value2");
object.put("ThirdKey", "Value3");
}
catch (JSONException e) {
e.printStackTrace();
}
The output in the log is: {"ThirdKey":"Value3","__type":"Value1","SecondKey":"Value2"}
Now, I understand the implementation uses a HashMap and so the order is not maintained. But when calling a WCF service the key __type IS REQUIRED to be first for the deserialization to occur on the service. How can I achieve this? For sometime, I have been using plain Strings to send to the webservice which works fine. However, this is not acceptable as it is ugly and hard to maintain.
Please advice, either from the Java side or WCF side how I can tackle this problem.
Thanks!