0

Working with StringBuilder came across a problem. I need to put a StringBuilder in JSONArray, but when i did my string is shuffled. How can I fix this?

My line before:

[{"id":"iprod","name":"soup","categoryId":"categoryId123","categoryName":"soup","imageUrl":"c/image","price":5.0,"weight":123.0,"ingredients":["chicken","rice"],"nutrition":{"energy":{"kilojoules":123,"kilocalories":123},"fat":123,"carbohydrate":123,"protein":123},"currency":"123"}]

My line after:

[{"nutrition":{"protein":123,"fat":123,"carbohydrate":123,"energy":{"kilojoules":123,"kilocalories":123}},"price":5,"imageUrl":"c/image","name":"soup","weight":123,"ingredients":["chicken","rice"],"currency":"123","id":"iprod","categoryName":"soup","categoryId":"categoryId123"}]

Code:

StringBuilder sb = new StringBuilder();

String line;
while ((line = br.readLine()) != null) {
    sb.append(line);
}

br.close();

System.out.println(sb.toString());
JSONArray responseObject = new JSONArray(sb.toString());
System.out.println(responseObject.get(0));
Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Mefisto_Fell
  • 876
  • 1
  • 10
  • 30
  • Your string doesn't represent JSONArray `[..]` but JSONObject `{..}` which some libraries are implementing by using/extending HashMap which doesn't preserve order of insertion of key-value pairs. – Pshemo Dec 14 '18 at 16:18
  • 1
    Possible duplicate: [JSON order mixed up](https://stackoverflow.com/q/3948206) – Pshemo Dec 14 '18 at 16:31
  • The array, `["chicken", "rice"]` maintains its order. The `key:value` pairs of the JSON Objects `{ "key1": "value1", "key2": "value2" }` have no order and should not be expected to maintain one. – xtratic Dec 14 '18 at 16:42

1 Answers1

1

Your String is JSONObject not JSONArray.

From the JSON specification at http://www.json.org/

An object is an unordered set of name/value pairs

JSON libraries are free to rearrange the order of the elements as they see fit. GSON, a Java library developed by Google for handling JSON. And it will save the order for you:

This is the code:

import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class Test {

    public static void main(String[] args) {
        Gson gson = new Gson();

        String line = "{\"id\":\"iprod\",\"name\":\"soup\",\"categoryId\":\"categoryId123\",\"categoryName\":\"soup\",\"imageUrl\":\"c/image\",\"price\":5.0,\"weight\":123.0,\"ingredients\":[\"chicken\",\"rice\"],\"nutrition\":{\"energy\":{\"kilojoules\":123,\"kilocalories\":123},\"fat\":123,\"carbohydrate\":123,\"protein\":123},\"currency\":\"123\"}\n";

        System.out.println(line);
        JsonObject responseObject = gson.fromJson(line, JsonObject.class);
        System.out.println(responseObject.keySet());

    }

}
Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • No, actually this is JSONArray, I missed the brackets. I update my post – Mefisto_Fell Dec 14 '18 at 17:52
  • @Mefisto_Fell Yes, but what JSONArray stores at index 0 is JSONObject which doesn't need to preserve order of key-value pairs. – Pshemo Dec 14 '18 at 18:16
  • Yes, but what if there are several objects, for example, "id": "iprod" and "id": "iprod1" In JSONObject, only the first is saved, but I need to save everything and the order is also preserved – Mefisto_Fell Dec 14 '18 at 19:22