Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Creating object in Json format using java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 572
    Comment on it

    Json object:

    You easily write your object in json format with java . Json format is used because it becomes easy to exchange information in this form. It is easy to understand and easy to write.

    Lets see an example, 

    If you are using maven add the following dependency:


    or you can download the json-simple.jar file.

    First, lets do it in simple way:

    import org.json.simple.JSONObject;
    public class CreateJson {
    	public static void main(String args[]){    
    		JSONObject json=new JSONObject();    
    		json.put("id",new Integer(01));    



    We can create Json object by using Map also, lets see how,

    import java.util.HashMap;  
    import java.util.Map;  
    import org.json.simple.JSONValue;  
    public class CreateJson{    
    public static void main(String args[]){    
      Map<String,Object> map=new HashMap<String,Object>();    
      map.put("id",new Integer(04));    
      map.put("address","4 th Street, UK");   
      String json = JSONValue.toJSONString(map);// Converting map to Json


    {"address":"4 th Street, UK","name":"Mark","id":4}

    This way, Map can be directly used to give output in json format.


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: