我使用 struts2 做 Action 和 jquery 做 UI ...
I am using struts2 for Action and jquery for UI ...
我想知道如何将 Map 对象转换为 JSON 对象并将其发送回 UI,
I want to know how to convert a Map object to JSON object and send it back to UI ,
现在可以在 JSP 页面中打印普通的 java Map 对象了:
Now am able to print it in JSP page the normal java Map object :
{71=Heart XXX, 76=No Heart YYY}
但我希望它是这样的:
{71:Heart XXX, 76:No Heart YYY}
我将如何实现这一目标....?
How will i achieve this .... ?
试试 Gson:
Gson gson = new Gson();
String json = gson.toJson(yourMap);
不过,我不建议将这种代码放入 JSP 中.像这样的东西应该存在于像 Servlet 或 Action 类这样的控制器中.
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}
而是像正确的 JSON(带引号的名称,带引号的字符串值):
but rather proper JSON like (quoted names, quoted string values):
{"71":"Heart XXX", "76":"No Heart YYY"}
Gson 会输出后者.
Gson will output the latter.
这篇关于将 Java 对象转换为 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)
XMLHttpRequest 的 getResponseHeader() 的限制?Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)