1

Is their an easy way or library to convert a JSON String to a Java object such that I can easily reference the elements in a JSP page? I think Map's can be referenced with simple dot notation in JSP pages, so JSON -> Map object should work?

UPDATE: Thank you for all the JSON Java libraries. In particular, I'm looking for a library that makes it easy for consumption in JSP pages. That means either the Java object created has appropriate getter methods corresponding to names of JSON nodes (is this possible?) or there's some other mechanism which makes it easy like the Map object.

at.
  • 50,922
  • 104
  • 292
  • 461
  • 1
    Please see any of the ~10 links listed on the right hand side of the page, they contain a wealth of information about recommended JSON-Java binding tools, including Jackson, GSON and the json.org tools that all do precisely this. – Mark Elliot Sep 19 '11 at 16:55
  • In particular, look at [Converting JSON to Java](http://stackoverflow.com/questions/1688099/converting-json-to-java). – Mark Elliot Sep 19 '11 at 16:56
  • @Mark, those answers don't really help. Maybe I should've been more clear in my question. The idea is to make it easy to convert an arbitrary JSON String to something I can easily use in a JSP page. None of the solutions I've seen so far do this in any obvious way. – at. Sep 19 '11 at 19:09
  • *all* of those solutions can convert *arbitrary JSON* to a *Map*, which is precisely what you said you wanted. – Mark Elliot Sep 19 '11 at 19:32
  • ok that sounds good, the examples I saw showed data-binding to POJOs. I guess my assumption about JSP code easily referencing Maps with simple dot notation is correct? – at. Sep 19 '11 at 19:41

4 Answers4

2

Use Jackson.

Updated:

If you have an arbitrary json string Jackson can return a map object to access the properties values.

Here a simple example.

@Test
public void testJsonMap() throws JsonParseException, JsonMappingException, IOException {
    String json = "{\"number\":\"8119123912\",\"msg\":\"Hello world\"}";
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String,Object>>() { });
    System.out.println("number:" + map.get("number") + " msg:" + map.get("msg"));
}

Output:

number:8119123912 msg:Hello world

Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
  • 1
    I'm using Jackson actually already for other code, would love to know how I can use Jackson on an arbitrary JSON String to make it easy for JSP consumption! Do you have a link or an example? The way I use it now, I bind it to POJOs. – at. Sep 19 '11 at 19:13
  • in JSP, I'm able to use `Map`s very easy, so this is the solution I went with. Thanks. – at. Sep 20 '11 at 06:17
1

Try GSON, here is a tutorial.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • Doesn't really help. GSON seems to require knowing the layout of the JSON String ahead of time. – at. Sep 19 '11 at 19:10
0

This library should do what you want: http://www.json.org/java/

jmar777
  • 38,796
  • 11
  • 66
  • 64
0

DWR can be used for this purpose DWR - Easy Ajax for JAVA .

Lets consider this java class.

   class Employee
   {
      int id;
      String eName;
       // setters and getters                
   }   

In javascript JSON object

   var employee = {
                   id   : null,
                   name : null
                  };

This is the call to java method from javascript function.

   EmployeeUtil.getRow(employee,dwrData);

In getRow() of EmployeeUtil class, return type of method will be Employee.

   Employee getRow();

So using the setters of Employee set the data.

dwrData is the callback function.

   function dwrData(data) {
                            employee=data;                   
                          }

The data returned which is Employee bean will be in callback function.

Just initialize this in javascript JSON object.

Now the JSON object can be parsed and displayed in jsp.

This example shows Dynamically Editing a Table

Hope this helps ....

Srikanth Venkatesh
  • 2,812
  • 1
  • 20
  • 13