Merge JSON Object
JSON Simple is a java library for encoding and decoding JSON text.
Get the latest binaries from
http://code.google.com/p/json-simple/ .
The following example shows merging two JSON Objects.
package com.bethecoder.tutorials.json_simple.tests;
import java.util.Date;
import org.json.simple.JSONObject;
public class MergeJsonObjects {
/**
* @param args
*/
public static void main ( String [] args ) {
JSONObject jsonObj = new JSONObject () ;
jsonObj.put ( "name" , "Sriram" ) ;
jsonObj.put ( "age" , 2 ) ;
jsonObj.put ( "hobby" , "painting" ) ;
JSONObject jsonObj2 = new JSONObject () ;
jsonObj2.put ( "hobby" , "Singing" ) ;
jsonObj2.put ( "dob" , new Date ( 110 , 4 , 6 )) ;
jsonObj.putAll ( jsonObj2 ) ;
System.out.println ( jsonObj ) ;
}
}
It gives the following output,
{"dob":Thu May 06 00:00:00 IST 2010,"age":2,"name":"Sriram","hobby":"Singing"}