3

i want to have json as it is inserted in this code but it works something different! i first want CustomerID and then Name but this json gives the Name first and then CustomerID. i have inserted the json as i want but why it gives different result please help me..

    JSONObject json = new JSONObject();

    try {
        json.put("CustomerID", "069C21F1-EE87-4FB4-A129-478AEAA454FF");
        json.put("Name", "Name_" + (int) Math.random() * 1000);
} catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
  • im not using right now but in debugging i saw that order is not maintained and as per requirement i need them to b in order – Kailash Dabhi Oct 01 '12 at 11:20
  • why you should care about the order , the JsonObject uses a HashMap to store it data , so if you want a value you should provide a key for it – confucius Oct 01 '12 at 11:22
  • actually im posting this json to url and at the server it is being parsed in order so... – Kailash Dabhi Oct 01 '12 at 11:24

4 Answers4

10

That is the expected behavior of a JSONObject. By its definition, it says:

An object as an unordered set of name/value pairs

however if you want it to be ordered do this:

1. prepare a LinkedHashMap object with elements

2. convert it to JSONObject

Example:

Map obj = new LinkedHashMap();
obj.put("a", "String1");
obj.put("b", new Integer(1));
obj.put("c", new Boolean(true));
obj.put("d", "String2");
JSONObject json = new JSONObject(obj);

EDIT:

download this library:

https://github.com/douglascrockford/JSON-java

save all the files in a new package in your project

instead of using org.json.JSONObject use your.package.JSONObject which you added from the downloaded library.

now open JSONObject.java file and change HashMap() to LinkedHashMap() in the constructor

public JSONObject(Map map)
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
1

You can put that Name before CustomerID line to satisfy yourself, but that wont make a difference at all. Because the data from json is extracted in the form of key value pairs. Its independent of the order its placed in.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
0

You can use accumulative()

See android developpers refference

Miquel
  • 8,339
  • 11
  • 59
  • 82
0

A useful answer here

Gson if your friend. This will print the ordered map into an ordered JSON string.

I used the latest version of Gson (2.3.1), you can can download it via the following options at the bottom of this post.

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;

public class OrderedJson {
    public static void main(String[] args) {
        // Create a new ordered map.
        Map<String,String> myLinkedHashMap = new LinkedHashMap<String, String>();

        // Add items, in-order, to the map.
        myLinkedHashMap.put("1", "first");
        myLinkedHashMap.put("2", "second");
        myLinkedHashMap.put("3", "third");

        // Instantiate a new Gson instance.
        Gson gson = new Gson();

        // Convert the ordered map into an ordered string.
        String json = gson.toJson(myLinkedHashMap, LinkedHashMap.class);

        // Print ordered string.
        System.out.println(json); // {"1":"first","2":"second","3":"third"}
    }
}

Dependency Options Maven

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>

Gradle

compile 'com.google.code.gson:gson:2.3.1'

Or you can visit Maven Central for more download options.

Community
  • 1
  • 1
msoa
  • 1,339
  • 3
  • 14
  • 33