title: JAVA: Save data in JSON
body: <p>When you add data in <code>JSONObject</code> it will store in it's own way. 
  Here is the Example of what i am trying to convey.</p>
  
  <pre><code>JSONObject obj = new JSONObject();
  
  obj.put("metricname", "splunk-ui");
  obj.put("timestamp", 1234567890);
  obj.put("value",34);
  
  
  System.out.println(obj);
  </code></pre>
  
  <p>Above code snippet will give below output.</p>
  
  <pre><code>{
      "metricname": "splunk-ui",
      "value": 34,
      "timestamp": 1234567890
  }
  </code></pre>
  
  <p><strong>Here is the Problem :-</strong></p>
  
  <p>I add data in this sequence  :- <code>metricname</code> , <code>timestamp</code> , <code>value</code></p>
  
  <p>This is the display sequence :- <code>metricname</code> , <code>value</code> , <code>timestamp</code></p>
  
  <p>So , how do i enforce my data adding sequence in <code>JSONObject</code> ??</p>
  
  <p><strong>FYI :-</strong>  Doing this is mendatory as i will pass this JSON object to another API which can scan data in  <code>metricname</code> , <code>timestamp</code> , <code>value</code> only.</p>
  
  <p><strong>HERE I AM POSTING CODE SNIPPET WHICH I USED FOR SOLVING THIS PROBLEM :-</strong> </p>
  
  <p>I have used GSON library and this link to make this code work. 
  <a href="http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/JsonObject.html" rel="nofollow">GSON Documention</a></p>
  
  <pre><code>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);
  </code></pre>