Convert 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 a JSON object and send it back to the interface,

Now I can print a regular Java map object on the JSP page:

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

But I want it to be like this:

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

How can I achieve this ....?

+1
source share
4 answers

Try Gson :

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

I would not recommend putting this code in a JSP. Such things should be in the controller, such as the Servlet or Action class.

You also definitely do not want the output to be:

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

but rather the correct JSON (quoted names, quoted string values):

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

Gson will output the last.

+1
source

I personally use the struts2-json plugin for this. It is very easy to use, and you can easily convert the map to Json and vice versa through some struts.xml entries. Create a card and its recipients / setters.

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

Define the global result as

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

in your struts.xml along with adding an interceptor to the session stack.

 <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" /> <action name="YouAction" class="YourActionClass" method="executeMethod"> <result type="json"></result> </action> 

Further documentation can be found here.

+6
source

To add Ashish to the answer, i.e. after adding struts2-json-plugin.

I like to use struts2-conions-plugin where possible, so I have very little in my struts.xml and prefer to use mostly annotations instead.

In order for your action to be returned by json when using conventions, there are two steps: 1) for your action to use the json-default package, 2) Define the action as having a json result type.

JSON annotation 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 the values ​​are more complex than the primitives, and you want to trim the returned json, or sometimes you need to put several actions in one class (sometimes you return a complex structure and, by trimming it in a certain way, you can make your work with the client easier). To do this, we use include or exclude parameters.

Language Exception Example

Modify the above annotation of the result:

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

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

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

Example Using wild cards with exclude parameter No action code specified, useful for cropping complex objects

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

With the plugin, you can see that it is rather complicated than the xml method or annotations.

+2
source

There are tons of Java JSON libraries at http://www.json.org/ . I would look through one of them.

+1
source

Source: https://habr.com/ru/post/927025/


All Articles