Created
November 2, 2012 10:34
-
-
Save madan712/4000063 to your computer and use it in GitHub Desktop.
Java program to convert Java Object to JSON String and vice versa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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