Skip to content

Instantly share code, notes, and snippets.

@madan712
Created November 2, 2012 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madan712/4000063 to your computer and use it in GitHub Desktop.
Save madan712/4000063 to your computer and use it in GitHub Desktop.
Java program to convert Java Object to JSON String and vice versa
import java.util.ArrayList;
import java.util.HashMap;
import org.json.simple.JSONValue;
public class TestJSON {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> country = new HashMap<String, ArrayList<String>>();
ArrayList<String> city = new ArrayList<String>();
city.add("Mumbai");
city.add("Delhi");
city.add("kolkata");
country.put("India", city);
// convert from Java Object to JSON string
String jsonText = JSONValue.toJSONString(country);
System.out.println(jsonText);
// convert from JSON string to Java Object
HashMap<String, ArrayList<String>> jCountry = (HashMap<String, ArrayList<String>>) JSONValue.parse(jsonText);
System.out.println(jCountry.get("India"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment