JSON order mixed up

前端 未结 12 1299
感情败类
感情败类 2020-11-22 02:01

I\'ve a problem trying to make my page printing out the JSONObject in the order i want. In my code, I entered this:



        
12条回答
  •  温柔的废话
    2020-11-22 02:18

    from lemiorhan example i can solve with just change some line of lemiorhan's code use:

    JSONObject json = new JSONObject(obj);
    

    instead of this:

    JSONObject json = (JSONObject) obj
    

    so in my test code is :

    Map item_sub2 = new LinkedHashMap();
    item_sub2.put("name", "flare");
    item_sub2.put("val1", "val1");
    item_sub2.put("val2", "val2");
    item_sub2.put("size",102);
    
    JSONArray itemarray2 = new JSONArray();
    itemarray2.add(item_sub2);
    itemarray2.add(item_sub2);//just for test
    itemarray2.add(item_sub2);//just for test
    
    
    Map item_sub1 = new LinkedHashMap();
    item_sub1.put("name", "flare");
    item_sub1.put("val1", "val1");
    item_sub1.put("val2", "val2");
    item_sub1.put("children",itemarray2);
    
    JSONArray itemarray = new JSONArray();
    itemarray.add(item_sub1);
    itemarray.add(item_sub1);//just for test
    itemarray.add(item_sub1);//just for test
    
    Map item_root = new LinkedHashMap();
    item_root.put("name", "flare");
    item_root.put("children",itemarray);
    
    JSONObject json = new JSONObject(item_root);
    
    System.out.println(json.toJSONString());
    

提交回复
热议问题