{"id":38345,"date":"2024-11-26T08:28:52","date_gmt":"2024-11-26T00:28:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38345\/"},"modified":"2024-11-26T08:28:52","modified_gmt":"2024-11-26T00:28:52","slug":"java%e4%b8%adjson%e4%b8%b2%e6%80%8e%e4%b9%88%e8%bd%ac%e5%8c%96%e4%b8%ba%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38345\/","title":{"rendered":"java\u4e2djson\u4e32\u600e\u4e48\u8f6c\u5316\u4e3a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528 gson \u6216 jackson \u5e93\uff1a\u4f7f\u7528 gson \u5bfc\u5165 com.google.gson.gson\uff0c\u7528 new gson() \u521b\u5efa gson \u5bf9\u8c61\uff0c\u7528 fromjson(jsonstring, int[].class) \u89e3\u6790 json \u5b57\u7b26\u4e32\u4e3a int[] \u6570\u7ec4\uff1b\u4f7f\u7528 jackson \u5bfc\u5165 com.fasterxml.jackson.databind.objectmapper\uff0c\u7528 new objectmapper() \u521b\u5efa objectmap\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111413272024115.jpg\" class=\"aligncenter\" title=\"java\u4e2djson\u4e32\u600e\u4e48\u8f6c\u5316\u4e3a\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2djson\u4e32\u600e\u4e48\u8f6c\u5316\u4e3a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u4ea4\u6362\u7684\u6587\u672c\u683c\u5f0f\u3002\u5728 Java \u4e2d\uff0c\u5904\u7406 JSON \u6570\u636e\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a Java \u5bf9\u8c61\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<p><strong>\u4f7f\u7528 Gson<\/strong><\/p>\n<p>Gson\uff08Google JSON\uff09\u662f\u4e00\u4e2a\u6d41\u884c\u7684 Java \u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 JSON \u6570\u636e\u4e0e Java \u5bf9\u8c61\u76f8\u4e92\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Gson \u8f6c\u6362 JSON \u5b57\u7b26\u4e32\u4e3a\u6570\u7ec4\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li><strong>\u5bfc\u5165 Gson \u5e93\uff1a<\/strong><\/li>\n<\/ol>\n<pre>import com.google.gson.Gson;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u521b\u5efa Gson \u5bf9\u8c61\uff1a<\/strong><\/li>\n<\/ol>\n<pre>Gson gson = new Gson();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u5c06 JSON \u4e3a\u6570\u7ec4\uff1a<\/strong><\/li>\n<\/ol>\n<pre>String jsonString = \"[1, 2, 3, 4]\";\nint[] array = gson.fromJson(jsonString, int[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65f6\uff0carray \u5c06\u662f\u4e00\u4e2a\u5305\u542b\u6574\u6570\u5143\u7d20 [1, 2, 3, 4] \u7684 int \u6570\u7ec4\u3002<\/p>\n<p><strong>\u4f7f\u7528 Jackson<\/strong><\/p>\n<p>Jackson \u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684 Java \u5e93\uff0c\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Jackson \u8f6c\u6362 JSON \u5b57\u7b26\u4e32\u4e3a\u6570\u7ec4\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u5bfc\u5165 Jackson \u5e93\uff1a<\/strong><\/li>\n<\/ol>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u521b\u5efa ObjectMapper \u5bf9\u8c61\uff1a<\/strong><\/li>\n<\/ol>\n<pre>ObjectMapper mapper = new ObjectMapper();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6570\u7ec4\uff1a<\/strong><\/li>\n<\/ol>\n<pre>String jsonString = \"[1, 2, 3, 4]\";\nint[] array = mapper.readValue(jsonString, int[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u540c\u7406\uff0carray \u5c06\u662f\u4e00\u4e2a\u5305\u542b\u6574\u6570\u5143\u7d20 [1, 2, 3, 4] \u7684 int \u6570\u7ec4\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>JSON \u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u4e00\u4e2a\u6709\u6548\u7684 JSON \u6570\u7ec4\uff0c\u5426\u5219\u8f6c\u6362\u5c06\u5931\u8d25\u3002<\/li>\n<li>\u6570\u7ec4\u5143\u7d20\u7684\u7c7b\u578b\u5fc5\u987b\u4e0e\u8f6c\u6362\u7684\u76ee\u6807\u7c7b\u578b\u5339\u914d\uff0c\u5426\u5219\u8f6c\u6362\u5c06\u5931\u8d25\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2djson\u4e32\u600e\u4e48\u8f6c\u5316\u4e3a\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528 gson \u6216 jackson \u5e93\uff1a\u4f7f\u7528 gson \u5bfc\u5165 com.google.gson.gson\uff0c\u7528 new gson() \u521b\u5efa gson \u5bf9\u8c61\uff0c\u7528 fromjson(jsonstring, int[].class) \u89e3\u6790 json \u5b57\u7b26\u4e32\u4e3a int[] \u6570\u7ec4\uff1b\u4f7f\u7528 jackson \u5bfc\u5165 com.fasterxml.jackson.databind.objectmapper\uff0c\u7528 new objectmapper() \u521b\u5efa objectmap \u5982\u4f55\u5728 Java \u4e2d\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4 JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u4ea4\u6362\u7684\u6587\u672c\u683c\u5f0f\u3002\u5728 Java \u4e2d\uff0c\u5904\u7406 JSON \u6570\u636e\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a Java \u5bf9\u8c61\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 \u4f7f\u7528 Gson Gson\uff08Google JSON\uff09\u662f\u4e00\u4e2a\u6d41\u884c\u7684 Java \u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 JSON \u6570\u636e\u4e0e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38345","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38345"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38345\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}