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.

5
  • 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. Commented Sep 19, 2011 at 16:55
  • In particular, look at Converting JSON to Java. Commented Sep 19, 2011 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. Commented Sep 19, 2011 at 19:09
  • all of those solutions can convert arbitrary JSON to a Map, which is precisely what you said you wanted. Commented Sep 19, 2011 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? Commented Sep 19, 2011 at 19:41

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
in JSP, I'm able to use Maps very easy, so this is the solution I went with. Thanks.
1

Try GSON, here is a tutorial.

1 Comment

Doesn't really help. GSON seems to require knowing the layout of the JSON String ahead of time.
0

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

Comments

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 ....

3 Comments

I don't see immediately how to make this easy to consume in a JSP page. Do you have an example in JSP code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.