<small id='ZOyQe'></small><noframes id='ZOyQe'>

    <tfoot id='ZOyQe'></tfoot>

      <i id='ZOyQe'><tr id='ZOyQe'><dt id='ZOyQe'><q id='ZOyQe'><span id='ZOyQe'><b id='ZOyQe'><form id='ZOyQe'><ins id='ZOyQe'></ins><ul id='ZOyQe'></ul><sub id='ZOyQe'></sub></form><legend id='ZOyQe'></legend><bdo id='ZOyQe'><pre id='ZOyQe'><center id='ZOyQe'></center></pre></bdo></b><th id='ZOyQe'></th></span></q></dt></tr></i><div id='ZOyQe'><tfoot id='ZOyQe'></tfoot><dl id='ZOyQe'><fieldset id='ZOyQe'></fieldset></dl></div>
        <legend id='ZOyQe'><style id='ZOyQe'><dir id='ZOyQe'><q id='ZOyQe'></q></dir></style></legend>
        • <bdo id='ZOyQe'></bdo><ul id='ZOyQe'></ul>

        将 Java 对象转换为 JSON?

        时间:2023-10-12

        <small id='85oGP'></small><noframes id='85oGP'>

          <bdo id='85oGP'></bdo><ul id='85oGP'></ul>

            • <tfoot id='85oGP'></tfoot>

                  <tbody id='85oGP'></tbody>
                <legend id='85oGP'><style id='85oGP'><dir id='85oGP'><q id='85oGP'></q></dir></style></legend>

                  <i id='85oGP'><tr id='85oGP'><dt id='85oGP'><q id='85oGP'><span id='85oGP'><b id='85oGP'><form id='85oGP'><ins id='85oGP'></ins><ul id='85oGP'></ul><sub id='85oGP'></sub></form><legend id='85oGP'></legend><bdo id='85oGP'><pre id='85oGP'><center id='85oGP'></center></pre></bdo></b><th id='85oGP'></th></span></q></dt></tr></i><div id='85oGP'><tfoot id='85oGP'></tfoot><dl id='85oGP'><fieldset id='85oGP'></fieldset></dl></div>
                  本文介绍了将 Java 对象转换为 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我使用 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模板网!

                  上一篇:struts2、ajax 和注入的 jquery 标签 下一篇:如何使用javascript调用Struts2 Action Class方法中的方

                  相关文章

                  最新文章

                    <bdo id='gj9DS'></bdo><ul id='gj9DS'></ul>
                1. <legend id='gj9DS'><style id='gj9DS'><dir id='gj9DS'><q id='gj9DS'></q></dir></style></legend>

                      <i id='gj9DS'><tr id='gj9DS'><dt id='gj9DS'><q id='gj9DS'><span id='gj9DS'><b id='gj9DS'><form id='gj9DS'><ins id='gj9DS'></ins><ul id='gj9DS'></ul><sub id='gj9DS'></sub></form><legend id='gj9DS'></legend><bdo id='gj9DS'><pre id='gj9DS'><center id='gj9DS'></center></pre></bdo></b><th id='gj9DS'></th></span></q></dt></tr></i><div id='gj9DS'><tfoot id='gj9DS'></tfoot><dl id='gj9DS'><fieldset id='gj9DS'></fieldset></dl></div>

                      <small id='gj9DS'></small><noframes id='gj9DS'>

                      <tfoot id='gj9DS'></tfoot>