1

I have written some code;

JSONArray ja = new JSONArray();
     try {
        ResultSetMetaData metaData = rslt.getMetaData();
        while ( rslt.next() ){
            JSONObject jo = new JSONObject();
            LinkedHashMap<String, String> jsonOrderedMap = new LinkedHashMap<String, String>();

            for(int i = 0 ; i < metaData.getColumnCount(); i++){
                jsonOrderedMap.put(metaData.getColumnName(i+1), rslt.getString(i+1) );
            }
            System.out.println(jsonOrderedMap);
            /* This doesn't work */
            ja.put(jsonOrderedMap);
            //System.out.println(ja);

        }
     } catch (Exception e) {
    }

I'am tring to order my json object ( I know it is not supposed to ordered , but I need it) It works until when I try to put my jsonOrderedMap to json array. Before JsonArray, json object looks like this :

{CUSTOMER_SECTOR_ID=611, CUSTOMER_NO=0013114193, CUSTOMER_NAME=asdfasdfds}

After putting some jsonOrderedMap to jsonArray, It looks like this;

[{"CUSTOMER_NAME":"qweqwe","CUSTOMER_NO":"0000003124","CUSTOMER_SECTOR_ID":"611"},
 {"CUSTOMER_NAME":"MAD.","CUSTOMER_NO":"0000003133","CUSTOMER_SECTOR_ID":"611"}]

As you can see it is not same order. Do you have any idea to fix it ? I am using gson library.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
Sahin Yanlık
  • 1,171
  • 2
  • 11
  • 21
  • As JSON objects do not inherently have an order, you should use an array within your JSON object to ensure order. [link](http://stackoverflow.com/questions/6993645/ordered-jsonobject) – Sergio Martinez May 02 '14 at 13:16
  • Thanks for your suggession @SergioMartinez but I can use your offered link if I know the type exactly. If I don't know it ? I just need to put strings as you see there is no field name. I am still trying to do but nothing works. – Sahin Yanlık May 02 '14 at 13:45

2 Answers2

2

Fields in a JSON objects don't have any order, because they are supposed to be accessed by their name (key).

If you need to keep an order, you have to use a JSON array instead.

I can't see why you would need named fields to be ordered, but if you really need to do so, you can use an array containing the fields of your JSON object.

This post describes how to do so (thanks @SergioMartinez for the link, it saved me so much time!).

Community
  • 1
  • 1
Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • Yes I did exactly same as @SergioMartinez in javascript part, but in java I don't know where will it comes first. For example you try to create a datatable and in first field I am using checkbox and there must be id. But I can't be sure without ordering. – Sahin Yanlık May 02 '14 at 13:42
  • Can you post the code you use to read the JSON? You shouldn't be taking the *first field* but the **"CUSTOMER_SECTOR_ID" field**. That's the point of JSON field names, which are like keys in a map. If you need to display data in a table, then you have to know what the data look like, that includes the names of the fields. – Joffrey May 02 '14 at 13:44
  • But Customer_sector_id field will always be in change I bring it from another place. I will put Join fields to first object ob json so I can get with same order. I bring this json with user interaction. – Sahin Yanlık May 02 '14 at 13:53
0

Jackson JSON Processor saves order. Simple example of usage:

   static ObjectMapper o = new ObjectMapper();
   public static void main(String[] args) throws IOException 
   {
      List<Map> array = new ArrayList<Map>();
      Map<String, String> obj1 = new LinkedHashMap<String, String>();
      obj1.put("CUSTOMER_SECTOR_ID", "Id");
      obj1.put("CUSTOMER_NO", "No");
      obj1.put("CUSTOMER_NAME", "Name");
      array.add(obj1);
      System.out.println(o.writeValueAsString(array));
   }

Maven dependency

  <dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-mapper-asl</artifactId>
     <version>1.8.5</version>
  </dependency>
Ilya
  • 29,135
  • 19
  • 110
  • 158