Map is used to save key value pairs. So its very easy for us to generate json format from Map interface.

Step1: create map object

Step2: put some key value on map object

Step3: pass the map object into JSONObject


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

import org.json.JSONObject;

public class GenJsonMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String, String> map=new HashMap<String, String>();
		map.put("name", "raja");
		JSONObject json=new JSONObject(map);
		System.out.println(json.toString());
	}
}


output:

{"name":"raja"}