Robin Dijkhof
Robin Dijkhof

Reputation: 19288

public JSONObject (String json) in api19 scrambles order?

I was parsing a string to a JSONObject and noticed some weird behaviour. The orders of items get scrambled? Is this a bug or did i do something wrong?

enter image description here

Upvotes: 0

Views: 67

Answers (1)

fweigl
fweigl

Reputation: 22038

Your 'items' are key/value pairs in a JSON object. The order of key/value pairs within a JSON object is not guaranteed:

An object is an unordered set of name/value pairs.

If you need to preserve the order of those items you can put them in an array on the serializing side (probably a server):

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Official docs.

Upvotes: 2

Related Questions