Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add data to JSONObject

Tags:

java

json

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(function() {
  $('#example').dataTable( {
    "aoColumnDefs": [
     { "aDataSort": [ 0, 1 ], "aTargets": [ 0 ] },
     { "aDataSort": [ 1, 0 ], "aTargets": [ 1 ] },
     { "aDataSort": [ 2, 3, 4 ], "aTargets": [ 2 ] }
   ]
  } );
} );

My JSONObject that needs to incorporate the above example.

    JSONObject json = new JSONObject();
    json.put("aoColumnDefs", );
like image 370
Code Junkie Avatar asked Jun 21 '12 17:06

Code Junkie


People also ask

How do I add data to an existing JSON object?

Use push() method to add JSON object to existing JSON array in JavaScript. Just do it with proper array of objects .

How do I change the value of a JSON object?

Array value of a JSON object can be modified. It can be simply done by modifying the value present at a given index. Note: If value is modified at an index which is out of the array size, then the new modification will not replace anything in the original information but rather will be an add-on.

How do I add a list to a JSON object in Java?

JSONObject obj = new JSONObject(); List<String> sList = new ArrayList<String>(); sList. add("val1"); sList. add("val2"); obj. put("list", sList);


3 Answers

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()); 
like image 172
Francisco Spaeth Avatar answered Sep 23 '22 21:09

Francisco Spaeth


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); 
like image 37
Yoni Avatar answered Sep 23 '22 21:09

Yoni


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" ) 
    )
);      
like image 30
BuvinJ Avatar answered Sep 24 '22 21:09

BuvinJ