{"id":39316,"date":"2024-11-26T10:34:16","date_gmt":"2024-11-26T02:34:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39316\/"},"modified":"2024-11-26T10:34:16","modified_gmt":"2024-11-26T02:34:16","slug":"%e6%80%8e%e4%b9%88%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aajson%e6%95%b0%e7%bb%84-java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39316\/","title":{"rendered":"\u600e\u4e48\u521b\u5efa\u4e00\u4e2ajson\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 java \u521b\u5efa json \u6570\u7ec4\uff1f\u5bfc\u5165\u5fc5\u8981\u7684\u5305\uff1aorg.json.jsonarray\u3002\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 jsonarray \u5bf9\u8c61\u3002\u4f7f\u7528 put() \u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u503c\u3002\u8c03\u7528 tostring() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111606241798648.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u521b\u5efa\u4e00\u4e2ajson\u6570\u7ec4 java\u63d2\u56fe\" alt=\"\u600e\u4e48\u521b\u5efa\u4e00\u4e2ajson\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u521b\u5efa JSON \u6570\u7ec4<\/strong><\/p>\n<p>\u4e00\u4e2a JSON \u6570\u7ec4\u662f\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u503c\u3002\u6bcf\u4e2a\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684 JSON \u6570\u636e\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5bf9\u8c61\u6216\u5176\u4ed6\u6570\u7ec4\u3002<\/p>\n<p><strong>\u521b\u5efa JSON \u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JSONArray \u7c7b\u6765\u521b\u5efa JSON \u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u7684 JSON \u6570\u7ec4\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\n<p>\u5bfc\u5165\u5fc5\u8981\u7684\u5305\uff1a<\/p>\n<pre>import org.json.JSONArray;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 JSONArray \u5bf9\u8c61\uff1a<\/p>\n<pre>JSONArray array = new JSONArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u4f7f\u7528 put() \u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u503c\uff1a<\/p>\n<pre>array.put(\"Item 1\");\narray.put(\"Item 2\");\narray.put(\"Item 3\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u8c03\u7528 toString() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>String json = array.toString();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>[\"Item 1\",\"Item 2\",\"Item 3\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528 put() \u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5411 JSON \u6570\u7ec4\u4e2d\u6dfb\u52a0\u503c\uff1a<\/p>\n<ul>\n<li> <strong>put(int index, Object value)\uff1a<\/strong>\u5728\u6307\u5b9a\u7d22\u5f15\u5904\u63d2\u5165\u503c\u3002<\/li>\n<li> <strong>add(Object value)\uff1a<\/strong>\u5728\u6570\u7ec4\u672b\u5c3e\u6dfb\u52a0\u503c\u3002<\/li>\n<li> <strong>addAll(JSONArray otherArray)\uff1a<\/strong>\u5c06\u53e6\u4e00\u4e2a JSON \u6570\u7ec4\u7684\u6240\u6709\u503c\u6dfb\u52a0\u5230\u6b64\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u521b\u5efa\u4e00\u4e2a java\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>\u5982\u4f55\u4f7f\u7528 java \u521b\u5efa json \u6570\u7ec4\uff1f\u5bfc\u5165\u5fc5\u8981\u7684\u5305\uff1aorg.json.jsonarray\u3002\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 jsonarray \u5bf9\u8c61\u3002\u4f7f\u7528 put() \u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u503c\u3002\u8c03\u7528 tostring() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\u3002 \u5982\u4f55\u4f7f\u7528 Java \u521b\u5efa JSON \u6570\u7ec4 \u4e00\u4e2a JSON \u6570\u7ec4\u662f\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u503c\u3002\u6bcf\u4e2a\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684 JSON \u6570\u636e\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5bf9\u8c61\u6216\u5176\u4ed6\u6570\u7ec4\u3002 \u521b\u5efa JSON \u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JSONArray \u7c7b\u6765\u521b\u5efa JSON \u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u7684 JSON \u6570\u7ec4\u7684\u6b65\u9aa4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5bfc\u5165\u5fc5\u8981\u7684\u5305\uff1a import org.json.JSONArray; \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 JSONArray \u5bf9\u8c61\uff1a JSONArray array = new JSONArray(); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 put() \u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u503c\uff1a array.put(&#8220;Item 1&#8221;); array.put(&#8220;Item [&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-39316","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39316","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=39316"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39316\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}