{"id":37594,"date":"2024-11-26T14:51:23","date_gmt":"2024-11-26T06:51:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37594\/"},"modified":"2024-11-26T14:51:23","modified_gmt":"2024-11-26T06:51:23","slug":"java%e4%b8%adjson%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37594\/","title":{"rendered":"java\u4e2djson\u6570\u7ec4\u600e\u4e48\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d json \u6570\u7ec4\u7531 jsonarray \u7c7b\u521b\u5efa\uff0c\u53ef\u6dfb\u52a0\u5143\u7d20\uff08\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5d4c\u5957 json \u5bf9\u8c61\/\u6570\u7ec4\uff09\uff0c\u5e76\u901a\u8fc7 get() \u65b9\u6cd5\u83b7\u53d6\u5143\u7d20\uff0c\u901a\u8fc7 remove() \u65b9\u6cd5\u79fb\u9664\u5143\u7d20\uff0c\u6700\u540e\u901a\u8fc7 tostring() \u65b9\u6cd5\u8f6c\u6362 json \u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111307363817002.jpg\" class=\"aligncenter\" title=\"java\u4e2djson\u6570\u7ec4\u600e\u4e48\u7ec4\u63d2\u56fe\" alt=\"java\u4e2djson\u6570\u7ec4\u600e\u4e48\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d JSON \u6570\u7ec4\u7684\u7ec4\u6210<\/strong><\/p>\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e\u6e90\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002JSON \u6570\u7ec4\u662f JSON \u6570\u636e\u7ed3\u6784\u7684\u4e00\u79cd\uff0c\u7528\u4e8e\u5b58\u50a8\u6709\u5e8f\u7684\u5143\u7d20\u96c6\u5408\u3002<\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JSONArray \u7c7b\u6765\u521b\u5efa\u548c\u64cd\u4f5c JSON \u6570\u7ec4\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u7ec4\u5efa\u4e00\u4e2a JSON \u6570\u7ec4\uff1a<\/p>\n<ol>\n<li><strong>\u521b\u5efa JSON \u6570\u7ec4\u5bf9\u8c61\uff1a<\/strong><\/li>\n<\/ol>\n<pre>JSONArray jsonArray = new JSONArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u5143\u7d20\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u901a\u8fc7 put() \u65b9\u6cd5\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002\u5143\u7d20\u53ef\u4ee5\u662f\u5404\u79cd\u7c7b\u578b\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u6216\u5d4c\u5957 JSON \u5bf9\u8c61\u548c\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>jsonArray.put(\"\u5143\u7d20 1\");\njsonArray.put(123);\njsonArray.put(true);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u5143\u7d20\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u901a\u8fc7 get() \u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<pre>String element = jsonArray.getString(0);\nint number = jsonArray.getInt(1);\nboolean bool = jsonArray.getBoolean(2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u79fb\u9664\u5143\u7d20\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u901a\u8fc7 remove() \u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u79fb\u9664\u5143\u7d20\u3002<\/p>\n<pre>jsonArray.remove(0);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u8fd4\u56de JSON \u5b57\u7b26\u4e32\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u4f7f\u7528 toString() \u65b9\u6cd5\u53ef\u4ee5\u5c06 JSON \u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/p>\n<pre>String jsonString = jsonArray.toString();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\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>java \u4e2d json \u6570\u7ec4\u7531 jsonarray \u7c7b\u521b\u5efa\uff0c\u53ef\u6dfb\u52a0\u5143\u7d20\uff08\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5d4c\u5957 json \u5bf9\u8c61\/\u6570\u7ec4\uff09\uff0c\u5e76\u901a\u8fc7 get() \u65b9\u6cd5\u83b7\u53d6\u5143\u7d20\uff0c\u901a\u8fc7 remove() \u65b9\u6cd5\u79fb\u9664\u5143\u7d20\uff0c\u6700\u540e\u901a\u8fc7 tostring() \u65b9\u6cd5\u8f6c\u6362 json \u5b57\u7b26\u4e32\u3002 Java \u4e2d JSON \u6570\u7ec4\u7684\u7ec4\u6210 JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e\u6e90\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002JSON \u6570\u7ec4\u662f JSON \u6570\u636e\u7ed3\u6784\u7684\u4e00\u79cd\uff0c\u7528\u4e8e\u5b58\u50a8\u6709\u5e8f\u7684\u5143\u7d20\u96c6\u5408\u3002 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JSONArray \u7c7b\u6765\u521b\u5efa\u548c\u64cd\u4f5c JSON \u6570\u7ec4\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u7ec4\u5efa\u4e00\u4e2a JSON \u6570\u7ec4\uff1a \u521b\u5efa JSON \u6570\u7ec4\u5bf9\u8c61\uff1a JSONArray jsonArray = new JSONArray(); \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0\u5143\u7d20\uff1a \u53ef\u4ee5\u901a\u8fc7 put() \u65b9\u6cd5\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002\u5143\u7d20\u53ef\u4ee5\u662f\u5404\u79cd\u7c7b\u578b\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u6216\u5d4c\u5957 JSON \u5bf9\u8c61\u548c\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b jsonArray.put(&#8220;\u5143\u7d20 1&#8221;); jsonArray.put(123); jsonArray.put(true); [&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-37594","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37594","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=37594"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37594\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}