Udi
Udi

Reputation: 1110

JSON Parsing problem

Hey, Im trying to parse the following JSON data:

{"chat":
{"link":
[{"@rel":"next","@ref":"http"}],
"events":
{"link2":
[{"@rel":"next","@ref":"http"}]}
}}

The code that reads the data is (where 'a' is the JSON as String):

  JSONObject jsonObject1 = new JSONObject(a);
  JSONObject jsonObject = jsonObject1.getJSONObject("chat");

So the structure (at least the way I intended) is:

<chat>
    <link>
    <events>
        <link2>
    </events>
</chat<

But, after getJsonObject("chat"), jsonObject equals to:

{"chat":{"events":{"link2":[{"@ref":"http","@rel":"next"}]},"link":[{"@ref":"http","@rel":"next"}]}}

What am I missing? Why does the data flips and the structure changes?

Upvotes: 1

Views: 257

Answers (1)

Manuel Salvadores
Manuel Salvadores

Reputation: 16525

The properties in a JSON object are not sorted. From the JSON site:

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

(My emphasis) Therefore the position of link and event are irrelevant for the parser. Bottom line, link and event are at the same level therefore they can be shifted and wherever order matters use arrays in JSON ... [].

Upvotes: 3

Related Questions