Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

java - Does the sequence of the values matter in a JSON object?

I have a JSON object which I have constructed within my Java program.

JSONObject jObj = {"AAA:aaa","BBB:bbb","CCC:ccc"}

I am sending this object to a server in which it expects the JSON object in the following type.

{"BBB:bbb", "AAA:aaa", "CCC:ccc"}

My question is that does the order of the JSON object really matters on the server side? If yes, how can I change the order?

question from:https://stackoverflow.com/questions/16870416/does-the-sequence-of-the-values-matter-in-a-json-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My question is that does the order of the JSON object really matters on the server side?

It should not matter. According to various JSON specifications, the order of the attributes is not significant. For example:

"An object is an unordered set of name/value pairs." (Source json.org)

"An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array." (Source RFC 7159)

Unfortunately, there are nitwits out there1 who ignore that aspect of the specs, and place some significance on the order of the attributes. (The mistake is usually made when there is a disconnect between the people specifying the APIs and those implementing them, and the people doing the specification work don't really understand JSON.)

Fortunately, the chances are that whoever designed / implemented the server didn't make that mistake. Most Java JSON parsers I've come across don't preserve the attribute order when parsing ... by default2. It would be hard to accidentally implement a server where the order of the JSON attributes being parsed was significant.

If yes, how can i change the order?

With difficulty, I fear:

  • You could generate the JSON by hand.
  • There is at least one JSON for java implementation3 that allows you to supply the Map object that holds a JSON object's attributes. If you use a LinkedHashMap or TreeMap, it should retain the insertion order or the lexical order of the attribute keys.

1 - For example, the nitwits that this poor developer was working for ... https://stackoverflow.com/a/4515863/139985

2 - RFC 7159 also says this: "JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software. Implementations whose behavior does not depend on member ordering will be interoperable in the sense that they will not be affected by these differences.". By my reading, this recommends that JSON libraries should hide any order of the pairs from application code.

3 - JSON-simple : https://code.google.com/p/json-simple/. There could be others too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...