tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > JSON JAVA > Map to JSON

Map to JSON 

JSON (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. Douglas Crockford has provided a reference implementation of JSON in Java at http://json.org/ useful for JSON serialization and deserialization. Click here to download the compiled library. The following example shows converting a Map to JSON object.

File Name  :  
com/bethecoder/tutorials/json_java/tests/Map2Json.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.json_java.tests;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

public class Map2Json {

  /**
   @param args
   @throws JSONException 
   */
  public static void main(String[] argsthrows JSONException {

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name""Sriram");
    map.put("age"2);
    map.put("dob"new Date(11046));
    map.put("hobby""painting");
    
    JSONObject obj = new JSONObject(map);
    System.out.println(obj.toString(2))//pretty print with indent
  }

}
   

It gives the following output,
{
  "name": "Sriram",
  "age": 2,
  "dob": "Thu May 06 00:00:00 IST 2010",
  "hobby": "painting"
}



 
  


  
bl  br