{"id":37311,"date":"2024-11-26T08:57:01","date_gmt":"2024-11-26T00:57:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37311\/"},"modified":"2024-11-26T08:57:01","modified_gmt":"2024-11-26T00:57:01","slug":"%e6%80%8e%e4%b9%88%e5%ae%9a%e4%b9%89json%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84-java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37311\/","title":{"rendered":"\u600e\u4e48\u5b9a\u4e49json\u5bf9\u8c61\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0cjson \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5305\u542b json \u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u4f7f\u7528 jsonarray \u7c7b\u8868\u793a\uff0c\u53ef\u901a\u8fc7 put() \u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\uff0c\u901a\u8fc7 get() \u65b9\u6cd5\u83b7\u53d6 json \u5bf9\u8c61\uff0c\u5177\u4f53\u793a\u4f8b\u53ef\u89c1\u672c\u6587\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111303511844275.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u5b9a\u4e49json\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" alt=\"\u600e\u4e48\u5b9a\u4e49json\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5b9a\u4e49 JSON \u5bf9\u8c61\u6570\u7ec4\u5728 Java \u4e2d<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0cJSON \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5305\u542b JSON \u5bf9\u8c61\u7684\u6570\u7ec4\u3002JSON \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a JSON \u5bf9\u8c61\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>JSONArray jsonArray = new JSONArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>JSONArray \u662f org.json \u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u8868\u793a\u4e00\u4e2a JSON \u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>\u6dfb\u52a0 JSON \u5bf9\u8c61<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 put() \u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0 JSON \u5bf9\u8c61\uff1a<\/p>\n<pre>jsonArray.put(new JSONObject());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 put() \u65b9\u6cd5\u6dfb\u52a0\u5176\u4ed6 JSON \u503c\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u6216\u5176\u4ed6 JSON \u6570\u7ec4\u3002<\/p>\n<p><strong>\u83b7\u53d6 JSON \u5bf9\u8c61<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 get() \u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u83b7\u53d6 JSON \u5bf9\u8c61\uff1a<\/p>\n<pre>JSONObject jsonObject = jsonArray.getJSONObject(index);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>index \u662f\u6570\u7ec4\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5728 Java \u4e2d\u521b\u5efa\u548c\u4f7f\u7528 JSON \u5bf9\u8c61\u6570\u7ec4\uff1a<\/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        \/\/ \u521b\u5efa\u4e00\u4e2a JSON \u5bf9\u8c61\u5e76\u6dfb\u52a0\u5230\u6570\u7ec4\n        JSONObject jsonObject1 = new JSONObject();\n        jsonObject1.put(\"name\", \"John\");\n        jsonObject1.put(\"age\", 30);\n        jsonArray.put(jsonObject1);\n        \n        \/\/ \u521b\u5efa\u53e6\u4e00\u4e2a JSON \u5bf9\u8c61\u5e76\u6dfb\u52a0\u5230\u6570\u7ec4\n        JSONObject jsonObject2 = new JSONObject();\n        jsonObject2.put(\"name\", \"Mary\");\n        jsonObject2.put(\"age\", 25);\n        jsonArray.put(jsonObject2);\n        \n        \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a JSON \u5bf9\u8c61\n        JSONObject jsonObject = jsonArray.getJSONObject(0);\n        System.out.println(jsonObject.getString(\"name\")); \/\/ \u8f93\u51fa\uff1aJohn\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5b9a\u4e49json\u5bf9\u8c61\u6570\u7ec4 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>\u5728 java \u4e2d\uff0cjson \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5305\u542b json \u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u4f7f\u7528 jsonarray \u7c7b\u8868\u793a\uff0c\u53ef\u901a\u8fc7 put() \u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\uff0c\u901a\u8fc7 get() \u65b9\u6cd5\u83b7\u53d6 json \u5bf9\u8c61\uff0c\u5177\u4f53\u793a\u4f8b\u53ef\u89c1\u672c\u6587\u3002 \u5982\u4f55\u5b9a\u4e49 JSON \u5bf9\u8c61\u6570\u7ec4\u5728 Java \u4e2d \u5728 Java \u4e2d\uff0cJSON \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5305\u542b JSON \u5bf9\u8c61\u7684\u6570\u7ec4\u3002JSON \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a JSON \u5bf9\u8c61\u3002 \u8bed\u6cd5 JSONArray jsonArray = new JSONArray(); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b JSONArray \u662f org.json \u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u8868\u793a\u4e00\u4e2a JSON \u6570\u7ec4\u3002 \u6dfb\u52a0 JSON \u5bf9\u8c61 \u53ef\u4ee5\u4f7f\u7528 put() \u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0 JSON \u5bf9\u8c61\uff1a jsonArray.put(new JSONObject()); \u767b\u5f55\u540e\u590d\u5236 \u4e5f\u53ef\u4ee5\u4f7f\u7528 [&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-37311","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37311","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=37311"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37311\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}