{"id":66611,"date":"2025-05-03T09:01:55","date_gmt":"2025-05-03T01:01:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66611\/"},"modified":"2025-05-03T09:01:55","modified_gmt":"2025-05-03T01:01:55","slug":"java%e6%80%8e%e4%b9%88%e6%8b%bc%e4%b8%80%e4%b8%aajson%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66611\/","title":{"rendered":"java\u600e\u4e48\u62fc\u4e00\u4e2ajson\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java\u62fc\u63a5json\u6570\u7ec4\u53ef\u4f7f\u7528jsonarray\u548cjsonobject\u7c7b\u5b9e\u73b0\uff1a1. \u521b\u5efajsonarray\u5bf9\u8c61\u5b58\u50a8\u6570\u7ec4\uff1b2. \u4f7f\u7528put()\u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\uff1b3. \u53ef\u9009\u521b\u5efajsonobject\u5bf9\u8c61\u5e76\u6dfb\u52a0\u5230jsonarray\u4e2d\u5d4c\u5957\uff1b4. \u6dfb\u52a0jsonobject\u5bf9\u8c61\u5230jsonarray\uff1b5. \u4f7f\u7528tostring()\u65b9\u6cd5\u62fc\u63a5json\u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111610185745736.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u62fc\u4e00\u4e2ajson\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u62fc\u4e00\u4e2ajson\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u62fc\u63a5 JSON \u6570\u7ec4<\/strong><\/p>\n<p>\u62fc\u63a5 JSON \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 Java \u7684 JSONArray \u548c JSONObject \u7c7b\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a JSONArray \u5bf9\u8c61<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>JSONArray \u5bf9\u8c61\u7528\u4e8e\u5b58\u50a8 JSON \u6570\u7ec4\u3002<\/p>\n<pre>import org.json.JSONArray;\n\nJSONArray jsonArray = new JSONArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u6dfb\u52a0\u5143\u7d20\u5230 JSONArray \u5bf9\u8c61<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 put() \u65b9\u6cd5\u5411 JSONArray \u5bf9\u8c61\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55 JSON \u6570\u636e\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u6216\u5d4c\u5957\u7684 JSONArray \u548c JSONObject \u5bf9\u8c61\u3002<\/p>\n<pre>jsonArray.put(\"John\");\njsonArray.put(123);\njsonArray.put(true);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u521b\u5efa\u4e00\u4e2a JSONObject \u5bf9\u8c61\uff08\u53ef\u9009\uff09<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u5d4c\u5957 JSON \u6570\u7ec4\uff0c\u53ef\u4ee5\u5728 JSONArray \u5bf9\u8c61\u4e2d\u6dfb\u52a0\u4e00\u4e2a JSONObject \u5bf9\u8c61\u3002<\/p>\n<pre>JSONObject jsonObject = new JSONObject();\njsonObject.put(\"name\", \"John\");\njsonObject.put(\"age\", 123);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u5c06 JSONObject \u5bf9\u8c61\u6dfb\u52a0\u5230 JSONArray \u5bf9\u8c61<\/strong><\/p>\n<pre>jsonArray.put(jsonObject);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u62fc\u63a5 JSON \u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u4e00\u65e6 JSONArray \u5bf9\u8c61\u521b\u5efa\u597d\uff0c\u53ef\u4ee5\u4f7f\u7528 toString() \u65b9\u6cd5\u5c06\u5176\u62fc\u63a5\u6210 JSON \u5b57\u7b26\u4e32\u3002<\/p>\n<pre>String jsonString = jsonArray.toString();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import org.json.JSONArray;\nimport org.json.JSONObject;\n\npublic class Main {\n  public static void main(String[] args) {\n    JSONArray jsonArray = new JSONArray();\n\n    jsonArray.put(\"John\");\n    jsonArray.put(123);\n    jsonArray.put(true);\n\n    JSONObject jsonObject = new JSONObject();\n    jsonObject.put(\"name\", \"John\");\n    jsonObject.put(\"age\", 123);\n\n    jsonArray.put(jsonObject);\n\n    String jsonString = jsonArray.toString();\n\n    System.out.println(jsonString);\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>[\"John\",123,true,{\"name\":\"John\",\"age\":123}]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u62fc\u4e00\u4e2a\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java\u62fc\u63a5json\u6570\u7ec4\u53ef\u4f7f\u7528jsonarray\u548cjsonobject\u7c7b\u5b9e\u73b0\uff1a1. \u521b\u5efajsonarray\u5bf9\u8c61\u5b58\u50a8\u6570\u7ec4\uff1b2. \u4f7f\u7528put()\u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\uff1b3. \u53ef\u9009\u521b\u5efajsonobject\u5bf9\u8c61\u5e76\u6dfb\u52a0\u5230jsonarray\u4e2d\u5d4c\u5957\uff1b4. \u6dfb\u52a0jsonobject\u5bf9\u8c61\u5230jsonarray\uff1b5. \u4f7f\u7528tostring()\u65b9\u6cd5\u62fc\u63a5json\u5b57\u7b26\u4e32\u3002 \u5982\u4f55\u4f7f\u7528 Java \u62fc\u63a5 JSON \u6570\u7ec4 \u62fc\u63a5 JSON \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 Java \u7684 JSONArray \u548c JSONObject \u7c7b\u6765\u5b9e\u73b0\u3002 \u6b65\u9aa4\uff1a 1. \u521b\u5efa\u4e00\u4e2a JSONArray \u5bf9\u8c61 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b JSONArray \u5bf9\u8c61\u7528\u4e8e\u5b58\u50a8 JSON \u6570\u7ec4\u3002 import org.json.JSONArray; JSONArray jsonArray = new JSONArray(); \u767b\u5f55\u540e\u590d\u5236 2. \u6dfb\u52a0\u5143\u7d20\u5230 JSONArray \u5bf9\u8c61 \u53ef\u4ee5\u4f7f\u7528 put() \u65b9\u6cd5\u5411 JSONArray \u5bf9\u8c61\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55 JSON \u6570\u636e\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u6216\u5d4c\u5957\u7684 JSONArray \u548c JSONObject \u5bf9\u8c61\u3002 jsonArray.put(&#8220;John&#8221;); jsonArray.put(123); [&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-66611","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66611","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=66611"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66611\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}