Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
698 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...