RSS Feed for This PostCurrent Article

Java: A Simple JSON Utility

Here is a simple JSON utility class that I used to do the followings

  • Convert JSON string to a Map and vice versa
  • Convert JSON string to list of maps and vice versa

This class encapsulate all the JSON string manipulation in a single Java class so that it frees you up from the actual JSON implementation. You can replace the JSON library just by changing this class later if needed.

You can download the source code here.

Here is the sample code snippet for testing

   1: /**
   2:    * Test Stub
   3:    *
   4:    * @param args
   5:    */
   6:   public static void main(String[] args) {
   7:       Map<String, String> map = new HashMap<String, String>();
   8:       map.put("name", "jason");
   9:       map.put("email", "[email protected]");
  10:  
  11:       // Convert map to JSON string
  12:       String jsonString = JsonUtil.getJsonStringFromMap(map);
  13:       System.out.println(jsonString);
  14:  
  15:       // Convert Json string to map
  16:       Map<String, String> newMap = JsonUtil.getMapFromJsonString(jsonString);
  17:       System.out.println(newMap);
  18:  
  19:  
  20:       Map<String, String> anotherMap = new HashMap<String, String>();
  21:       anotherMap.put("name", "alice");
  22:       anotherMap.put("email", "[email protected]");
  23:  
  24:       List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
  25:       mapList.add(map);
  26:       mapList.add(anotherMap);
  27:       // Convert to json string
  28:       jsonString = JsonUtil.getJsonStringFromList(mapList);
  29:       System.out.println(jsonString);
  30:  
  31:       // Convert Json string to list of map
  32:       List<Map<String, String>> newMapList = JsonUtil.getListFromJsonArray(jsonString);
  33:       System.out.println(newMapList);
  34:  
  35:  
  36:   }

 

The output

   1: {"email":"[email protected]","name":"jason"}
   2:  
   3: {[email protected], name=jason}
   4:  
   5: [{"email":"[email protected]","name":"jason"},{"email":"[email protected]","name":"alice"}]
   6:  
   7: [{[email protected], name=jason}, {[email protected], name=alice}]

 

The code uses the Jackson JSON library.


Trackback URL


RSS Feed for This Post4 Comment(s)

  1. Mike Whitman | Jul 24, 2009 | Reply

    org.codehaus.jackson.map.JavaTypeMapper

    This class seems to no longer be in Jackson 1.x.
    Any update on the class above.

  2. rahul | Aug 30, 2009 | Reply

    Which version of jackson json library are you using as Mark mentioned “org.codehaus.jackson.map.JavaTypeMapper” this class is not available in latest jars

  3. Cowtown Coder | Oct 28, 2009 | Reply

    Jackson 1.x counterpart would be ObjectMapper (under org.codehaus.jackson.map).

  4. Pedro Gomes | Mar 2, 2010 | Reply

    I replaced the lines in error by:

    The read lines:
    Replace the old object with ObjectMapper and use the readValue with the correspondent class. An example:

    ObjectMapper() .readValue(jf.createJsonParser(new StringReader(str)),ArrayList.class);

    The write lines:
    Just replace the name of the object and use the writeValue method. ex:

    ObjectMapper().writeValue(gen, aMap);

Sorry, comments for this entry are closed at this time.