0

When you add data in JSONObject it will store in it's own way. Here is the Example of what i am trying to convey.

JSONObject obj = new JSONObject();

obj.put("metricname", "splunk-ui");
obj.put("timestamp", 1234567890);
obj.put("value",34);


System.out.println(obj);

Above code snippet will give below output.

{
    "metricname": "splunk-ui",
    "value": 34,
    "timestamp": 1234567890
}

Here is the Problem :-

I add data in this sequence :- metricname , timestamp , value

This is the display sequence :- metricname , value , timestamp

So , how do i enforce my data adding sequence in JSONObject ??

FYI :- Doing this is mendatory as i will pass this JSON object to another API which can scan data in metricname , timestamp , value only.

HERE I AM POSTING CODE SNIPPET WHICH I USED FOR SOLVING THIS PROBLEM :-

I have used GSON library and this link to make this code work. GSON Documention

JsonObject jsonObject = new JsonObject();

jsonObject.addProperty("metric", "mihirmonani");
jsonObject.addProperty("timestamp", 1346846400);
jsonObject.addProperty("value", 14);

JsonArray jArray = new JsonArray();

JsonObject jObject = new JsonObject();

jObject.addProperty("host", "splunk");
jObject.addProperty("host1", "splunk1");

jsonObject.add("tags",jObject);

System.out.println(jsonObject);
mihir6692
  • 177
  • 1
  • 4
  • 19
  • The attributes of an JSON object have no order. That's not specific to Java or any implementation. If you need a specific order, you must use an array in JSON, not an object. Make sure that you understand the difference. – Thomas Uhrig Oct 02 '15 at 10:22

2 Answers2

0

You can't with a JSONObject the only way to keep the order is using a JSONArray

    [
      {"name" : "metricname", "value" : "splunk-ui"},
      {"name" : "value", "value" : 34},
      {"name" : "timestamp", "value" : 1234567890}
    ]
Lindus
  • 66
  • 5
0

Short answer: you can't. JSONObject uses a HashMap internally, which returns values depending on the order of the hash code.

You could use JSONWriter to write the values explicitly.

EDIT: To clarify, as Manu pointed out, you cannot retrieve the order after the values have been put in the JSONObject. My suggestion was to not put the values in the JSONObject to begin with, but use JSONWriter to write the values directly.

Patrick Simpson
  • 554
  • 4
  • 12
  • The reason is not how JSONObject is implemented. The reason is that JSON doesn't specify the order of attributes of an JSON object. That's how JSON works. Using the JSONWriter wouldn't change that. – Thomas Uhrig Oct 02 '15 at 10:20
  • Though it's correct that JSON doesn't specify any ordering, that in no way means an implementation doesn't have any effect on the order in which values are emitted. JSONObject is an implementation, not a specification, which emits values in the order they are iterated from the internal HashMap. JSONWriter writes values directly, so the order is exactly the way you write them. That the JSON parser is not correct in expecting a certain order only means that it's a bad JSON parser, not that you cannot emit values in the way it expects. – Patrick Simpson Oct 02 '15 at 10:26
  • @PatrickSimpson Sure, you can write values in some order, but never in the order they were inserted, because that order is unknown. – Manu Oct 02 '15 at 10:30
  • @Manu Good point. To clarify, I don't suggest using JSONWriter after putting the values in the JSONObject, but instead of putting them in there. – Patrick Simpson Oct 02 '15 at 10:33