Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...