Esh
Esh

Reputation: 856

Converting Java Object to JSON?

I am using struts2 for Action and jquery for UI ...

I want to know how to convert a Map object to JSON object and send it back to UI ,

Now am able to print it in JSP page the normal java Map object :

{71=Heart XXX, 76=No Heart YYY}

But i want it to be like this :

{71:Heart XXX, 76:No Heart YYY}

How will i achieve this .... ?

Upvotes: 1

Views: 14827

Answers (4)

Quaternion
Quaternion

Reputation: 10458

To add to Ashish's answer, that is after adding the struts2-json-plugin.

I like to use the struts2-conventions-plugin where possible as such I have very little in my struts.xml and prefer to use mostly annotations instead.

To make your action return json when using conventions there are two steps: 1) have your action use the json-default package, 2) Define the action as having a json result type.

JSON Annotations Example

package org.test.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
@ParentPackage("json-default")
@Result(type = "json")
public class JsonTest extends ActionSupport{
    private String name = "Hello World";
    private String language = "Java, I mean English";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
}

Sometimes values more complex than primitives and you will want to prune the json returned, or sometimes you will want to put multiple actions into a single class (sometimes you get back a complicated structure and by pruning it certain ways you can make your work on the client easier). To do this we use include or exclude parameters.

Example exclude language

Modify the above result annotation to be:

@Result(type = "json", params = {
    "excludeProperties",
    "language"})

Another way to achieve the above is to explicitly state what properties we do want:

@Result(type = "json", params = {
    "includeProperties",
    "name"})

Example Using wild cards with exclude parameter Action code not supplied, good for trimming complicated objects

@Result(type = "json", params = {
    "excludeProperties",
    "punches.*.punchesCollection, *.punchesCollection.*"}) 

You can see with the plugin it is pretty hard to get easier than either the xml or annotation method.

Upvotes: 2

Ash
Ash

Reputation: 1649

I personally use struts2-json plugin for this. It's very easy to use and you can easily convert Map to Json and vice versa through some struts.xml entries. Create a map and its getter/setters.

private Map<String, String> map= new HashMap<String, String>();

Define a global result as

 <result-type name="json" class="org.apache.struts2.json.JSONResult" default="false" />

in your struts.xml along with adding interceptor in your session stack.

<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />

<action name="YouAction" class="YourActionClass" method="executeMethod">
         <result type="json"></result>
</action>

More documentation can be found here

Upvotes: 6

Philipp Reichart
Philipp Reichart

Reputation: 20961

Try Gson:

Gson gson = new Gson();
String json = gson.toJson(yourMap);

I wouldn't recommend putting this kind of code into a JSP, though. Things like these should live in a controller like a Servlet or Action class.

You also most definitely don't want the output to be:

{71:Heart XXX, 76:No Heart YYY}

but rather proper JSON like (quoted names, quoted string values):

{"71":"Heart XXX", "76":"No Heart YYY"}

Gson will output the latter.

Upvotes: 1

Kasper Pedersen
Kasper Pedersen

Reputation: 516

There's a bunch of JSON libraries for Java on http://www.json.org/. I would check out one of those.

Upvotes: 1

Related Questions