
{"id":2723,"date":"2020-12-16T10:26:10","date_gmt":"2020-12-16T02:26:10","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2723"},"modified":"2021-01-01T14:36:10","modified_gmt":"2021-01-01T06:36:10","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bspringmvc%e7%9a%84json%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2723","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\u4e4bspringMVC\u7684json\u7c7b\u578b"},"content":{"rendered":"<h3>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684json\u7c7b\u578b<\/h3>\n<p><strong>1.jackson<\/strong><\/p>\n<pre>&lt;!-- jackson --&gt;\r\n&lt;dependency&gt;\r\n  &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;\r\n  &lt;version&gt;2.12.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>\u63a7\u5236\u5c42\uff1a<\/p>\n<pre>\/\/json\u67e5\u8be2\u6240\u6709\r\n@ResponseBody\r\n@RequestMapping(value = \"\/listjson\",method = RequestMethod.GET)\r\npublic Collection&lt;User&gt; listjson(){\r\n    return usersDao.getAll();\r\n}<\/pre>\n<p><strong>2.gson<\/strong><\/p>\n<p><!--more--><\/p>\n<pre>&lt;!-- gson  --&gt;\r\n&lt;dependency&gt;\r\n  &lt;groupId&gt;com.google.code.gson&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;gson&lt;\/artifactId&gt;\r\n  &lt;version&gt;2.8.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p><strong>3.jsonp<\/strong><\/p>\n<p>\u63a7\u5236\u5c42\uff1a<\/p>\n<pre>\/\/jsonp\r\n@RequestMapping(value = \"\/jsonp\",method = RequestMethod.GET)\r\npublic void jsonp(String callback, HttpServletResponse response){\r\n    Collection&lt;User&gt; list = usersDao.getAll();\r\n    Gson gson=new Gson();\r\n    String json = gson.toJson(list);\r\n    PrintWriter writer = null;\r\n    try {\r\n        writer = response.getWriter();\r\n    } catch (IOException e) {}\r\n    writer.println(callback+\"(\"+json+\")\");\r\n}<\/pre>\n<p>\u811a\u672c\uff1a<\/p>\n<pre>&lt;script&gt;\r\n    function showdata(data){\r\n        console.log(data);\r\n    }\r\n&lt;\/script&gt;\r\n&lt;script src=\"${pageContext.request.contextPath}\/admin\/jsonp?callback=showdata\"&gt;&lt;\/script&gt;<\/pre>\n<p><strong>json\u6570\u636e\u7c7b\u578b\u7684\u63d0\u4ea4<\/strong><\/p>\n<p>\u63a7\u5236\u5c42\uff1a<\/p>\n<pre>@RequestMapping(value = \"save-json\",method = RequestMethod.POST)\r\npublic String savejson(@RequestBody User user){\r\n    System.out.println(user);\r\n    System.out.println(\"savejson\");\r\n    usersDao.save(user);\r\n    return \"redirect:\/admin\/list\";\r\n}<\/pre>\n<p>\u6dfb\u52a0\u8868\u5355\u9875\uff1a<\/p>\n<p>\u5c06\u8868\u5355\u7684\u63d0\u4ea4\u6309\u94ae\u6539\u6210<\/p>\n<pre>&lt;button type=\"button\" id=\"jsonBtn\"&gt;submit&lt;\/button&gt;<\/pre>\n<pre>&lt;script src=\"${pageContext.request.contextPath}\/lib\/js\/jquery-3.3.1.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n    $(function(){\r\n        $(\"#jsonBtn\").click(function(){\r\n            var row={\r\n                username:$(\"#username\").val(),\r\n                password:$(\"#password\").val(),\r\n                age:$(\"#age\").val()\r\n            }\r\n            $.ajax({\r\n                url:'save-json',\r\n                type:'post',\r\n                contentType:'application\/json;charset=utf-8',\r\n                data:JSON.stringify(row)\r\n            });\r\n        });\r\n    });\r\n&lt;\/script&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684json\u7c7b\u578b 1.jackson &lt;!&#8211; jackson &#8212; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2723","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2723"}],"version-history":[{"count":4,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2723\/revisions"}],"predecessor-version":[{"id":2734,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2723\/revisions\/2734"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}