{"id":36991,"date":"2024-11-26T15:17:10","date_gmt":"2024-11-26T07:17:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36991\/"},"modified":"2024-11-26T15:17:10","modified_gmt":"2024-11-26T07:17:10","slug":"java%e6%80%8e%e4%b9%88%e8%a7%a3%e6%9e%90json%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36991\/","title":{"rendered":"java\u600e\u4e48\u89e3\u6790json\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java\u89e3\u6790json\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u4f7f\u7528jsonobject\u548cjsonarray\u7c7b\uff1a\u521b\u5efajsonobject\u5bf9\u8c61\uff0c\u89e3\u6790json\u5b57\u7b26\u4e32\u3002\u83b7\u53d6jsonarray\u5bf9\u8c61\uff0c\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u7684\u5c5e\u6027\u503c\u3002\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982gson\uff09\uff1a\u4f7f\u7528gson\u89e3\u6790json\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u5b9a\u5bf9\u8c61\u7684\u5217\u8868\u3002\u904d\u5386\u7528\u6237\u5217\u8868\uff0c\u83b7\u53d6\u6bcf\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111223002059060.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u89e3\u6790json\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u89e3\u6790json\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u89e3\u6790 JSON \u6570\u7ec4<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u65b9\u5f0f<\/strong><\/p>\n<p>\u4f7f\u7528 JSONObject \u548c JSONArray \u7c7b\uff1a<\/p>\n<pre>import org.json.JSONArray;\nimport org.json.JSONObject;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString jsonString = \"[{\"name\":\"John\", \"age\":30}, {\"name\":\"Mary\", \"age\":25}]\";\n\n\/\/ \u89e3\u6790 JSON \u5b57\u7b26\u4e32\u4e3a JSON \u5bf9\u8c61\nJSONObject jsonObject = new JSONObject(jsonString);\n\n\/\/ \u83b7\u53d6 JSON \u6570\u7ec4\nJSONArray jsonArray = jsonObject.getJSONArray(\"users\");\n\n\/\/ \u904d\u5386 JSON \u6570\u7ec4\nfor (int i = 0; i &lt; jsonArray.length(); i++) {\n    \/\/ \u83b7\u53d6\u6bcf\u4e2a JSON \u5bf9\u8c61\n    JSONObject user = jsonArray.getJSONObject(i);\n\n    \/\/ \u83b7\u53d6\u5355\u4e2a\u5c5e\u6027\u503c\n    String name = user.getString(\"name\");\n    int age = user.getInt(\"age\");\n\n    \/\/ \u6253\u5370\u7ed3\u679c\n    System.out.println(\"Name: \" + name + \", Age: \" + age);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 GSON \u7b49\u7b2c\u4e09\u65b9\u5e93\u6765\u89e3\u6790 JSON\uff1a<\/p>\n<pre>import com.google.gson.Gson;\nimport com.google.gson.reflect.TypeToken;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString jsonString = \"[{\"name\":\"John\", \"age\":30}, {\"name\":\"Mary\", \"age\":25}]\";\n\n\/\/ \u4f7f\u7528 GSON \u89e3\u6790 JSON \u5b57\u7b26\u4e32\nGson gson = new Gson();\nTypeToken&lt;ArrayList&lt;User&gt;&gt; token = new TypeToken&lt;&gt;() {};\nArrayList&lt;User&gt; users = gson.fromJson(jsonString, token.getType());\n\n\/\/ \u904d\u5386\u7528\u6237\u5217\u8868\nfor (User user : users) {\n    \/\/ \u83b7\u53d6\u5355\u4e2a\u5c5e\u6027\u503c\n    String name = user.getName();\n    int age = user.getAge();\n\n    \/\/ \u6253\u5370\u7ed3\u679c\n    System.out.println(\"Name: \" + name + \", Age: \" + age);\n}\n\n\/\/ \u5b9a\u4e49\u7528\u6237\u7c7b\nclass User {\n    private String name;\n    private int age;\n\n    \/\/ \u7701\u7565 getter \u548c setter \u65b9\u6cd5\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u89e3\u6790\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\u89e3\u6790json\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u4f7f\u7528jsonobject\u548cjsonarray\u7c7b\uff1a\u521b\u5efajsonobject\u5bf9\u8c61\uff0c\u89e3\u6790json\u5b57\u7b26\u4e32\u3002\u83b7\u53d6jsonarray\u5bf9\u8c61\uff0c\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u7684\u5c5e\u6027\u503c\u3002\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982gson\uff09\uff1a\u4f7f\u7528gson\u89e3\u6790json\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u5b9a\u5bf9\u8c61\u7684\u5217\u8868\u3002\u904d\u5386\u7528\u6237\u5217\u8868\uff0c\u83b7\u53d6\u6bcf\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002 \u5982\u4f55\u4f7f\u7528 Java \u89e3\u6790 JSON \u6570\u7ec4 \u76f4\u63a5\u65b9\u5f0f \u4f7f\u7528 JSONObject \u548c JSONArray \u7c7b\uff1a import org.json.JSONArray; import org.json.JSONObject; \/\/ JSON \u5b57\u7b26\u4e32 String jsonString = &#8220;[{&#8220;name&#8221;:&#8221;John&#8221;, &#8220;age&#8221;:30}, {&#8220;name&#8221;:&#8221;Mary&#8221;, &#8220;age&#8221;:25}]&#8221;; \/\/ \u89e3\u6790 JSON \u5b57\u7b26\u4e32\u4e3a JSON \u5bf9\u8c61 JSONObject jsonObject = new JSONObject(jsonString); \/\/ \u83b7\u53d6 JSON \u6570\u7ec4 JSONArray jsonArray = jsonObject.getJSONArray(&#8220;users&#8221;); \/\/ \u904d\u5386 JSON \u6570\u7ec4 for (int i = 0; i [&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-36991","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36991","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=36991"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36991\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}