javascript 将 Java 对象转换为 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12263468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Java Object to JSON?
提问by Esh
I am using struts2 for Action and jquery for UI ...
我将 struts2 用于 Action 和 jquery 用于 UI ...
I want to know how to convert a Map object to JSON object and send it back to UI ,
我想知道如何将 Map 对象转换为 JSON 对象并将其发送回 UI ,
Now am able to print it in JSP page the normal java Map object :
现在可以在 JSP 页面中打印普通的 java Map 对象:
{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 .... ?
我将如何实现这一目标......?
采纳答案by Philipp Reichart
Try Gson:
试试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.
不过,我不建议将这种代码放入 JSP 中。像这样的东西应该存在于像 Servlet 或 Action 类这样的控制器中。
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):
而是正确的 JSON 之类的(带引号的名称,带引号的字符串值):
{"71":"Heart XXX", "76":"No Heart YYY"}
Gson will output the latter.
Gson 将输出后者。
回答by Ashish Gupta
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.
我个人struts2-json
为此使用插件。它非常易于使用,您可以通过一些 struts.xml 条目轻松地将 Map 转换为 Json,反之亦然。创建地图及其 getter/setter。
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.
在您struts.xml
的会话堆栈中添加拦截器。
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
<action name="YouAction" class="YourActionClass" method="executeMethod">
<result type="json"></result>
</action>
回答by Quaternion
To add to Ashish's answer, that is after adding the struts2-json-plugin.
添加到 Ashish 的答案,这是在添加 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.
我喜欢尽可能使用 struts2-conventions-plugin,因为我的 struts.xml 中只有很少的内容,而更喜欢使用大部分注释。
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,有两个步骤:1) 让您的操作使用 json-default 包,2) 将操作定义为具有 json 结果类型。
JSON Annotations Example
JSON 注释示例
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.
有时值比原语更复杂,您需要修剪返回的 json,或者有时您想将多个操作放入单个类中(有时您会返回一个复杂的结构,并通过修剪某些方法可以使您的工作在客户更容易)。为此,我们使用包含或排除参数。
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 parameterAction 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.
您可以看到使用插件很难比 xml 或注释方法更容易。
回答by Kasper Pedersen
There's a bunch of JSON libraries for Java on http://www.json.org/. I would check out one of those.
http://www.json.org/上有一堆用于 Java 的 JSON 库。我会检查其中之一。