Add data to JSONObject

后端 未结 3 1746
刺人心
刺人心 2020-12-05 05:09

I\'m trying to figure out how to add the following data to my json object. Could someone show me how to do this.

Website Example

$(document).ready(f         


        
相关标签:
3条回答
  • 2020-12-05 05:27

    The answer is to use a JSONArray as well, and to dive "deep" into the tree structure:

    JSONArray arr = new JSONArray();
    arr.put (...); // a new JSONObject()
    arr.put (...); // a new JSONObject()
    
    JSONObject json = new JSONObject();
    json.put ("aoColumnDefs",arr);
    
    0 讨论(0)
  • 2020-12-05 05:35

    In order to have this result:

    {"aoColumnDefs":[{"aTargets":[0],"aDataSort":[0,1]},{"aTargets":[1],"aDataSort":[1,0]},{"aTargets":[2],"aDataSort":[2,3,4]}]}
    

    that holds the same data as:

      {
        "aoColumnDefs": [
         { "aDataSort": [ 0, 1 ], "aTargets": [ 0 ] },
         { "aDataSort": [ 1, 0 ], "aTargets": [ 1 ] },
         { "aDataSort": [ 2, 3, 4 ], "aTargets": [ 2 ] }
       ]
      }
    

    you could use this code:

        JSONObject jo = new JSONObject();
        Collection<JSONObject> items = new ArrayList<JSONObject>();
    
        JSONObject item1 = new JSONObject();
        item1.put("aDataSort", new JSONArray(0, 1));
        item1.put("aTargets", new JSONArray(0));
        items.add(item1);
        JSONObject item2 = new JSONObject();
        item2.put("aDataSort", new JSONArray(1, 0));
        item2.put("aTargets", new JSONArray(1));
        items.add(item2);
        JSONObject item3 = new JSONObject();
        item3.put("aDataSort", new JSONArray(2, 3, 4));
        item3.put("aTargets", new JSONArray(2));
        items.add(item3);
    
        jo.put("aoColumnDefs", new JSONArray(items));
    
        System.out.println(jo.toString());
    
    0 讨论(0)
  • 2020-12-05 05:38

    The accepted answer by Francisco Spaeth works and is easy to follow. However, I think that method of building JSON sucks! This was really driven home for me as I converted some Python to Java where I could use dictionaries and nested lists, etc. to build JSON with ridiculously greater ease.

    What I really don't like is having to instantiate separate objects (and generally even name them) to build up these nestings. If you have a lot of objects or data to deal with, or your use is more abstract, that is a real pain!

    I tried getting around some of that by attempting to clear and reuse temp json objects and lists, but that didn't work for me because all the puts and gets, etc. in these Java objects work by reference not value. So, I'd end up with JSON objects containing a bunch of screwy data after still having some ugly (albeit differently styled) code.

    So, here's what I came up with to clean this up. It could use further development, but this should help serve as a base for those of you looking for more reasonable JSON building code:

    import java.util.AbstractMap.SimpleEntry;
    import java.util.ArrayList;
    import java.util.List;
    import org.json.simple.JSONObject;
    
    //  create and initialize an object 
    public static JSONObject buildObject( final SimpleEntry... entries ) { 
        JSONObject object = new JSONObject();
        for( SimpleEntry e : entries ) object.put( e.getKey(), e.getValue() );
        return object;
    }
    
    //  nest a list of objects inside another                          
    public static void putObjects( final JSONObject parentObject, final String key,
                                   final JSONObject... objects ) { 
        List objectList = new ArrayList<JSONObject>();
        for( JSONObject o : objects ) objectList.add( o );
        parentObject.put( key, objectList );
    }   
    

    Implementation example:

    JSONObject jsonRequest = new JSONObject();
    putObjects( jsonRequest, "parent1Key",
        buildObject( 
            new SimpleEntry( "child1Key1", "someValue" )
          , new SimpleEntry( "child1Key2", "someValue" ) 
        )
      , buildObject( 
            new SimpleEntry( "child2Key1", "someValue" )
          , new SimpleEntry( "child2Key2", "someValue" ) 
        )
    );      
    
    0 讨论(0)
提交回复
热议问题