{"id":2731,"date":"2022-12-20T12:52:43","date_gmt":"2022-12-20T04:52:43","guid":{"rendered":"https:\/\/qaqaq.top\/?p=2731"},"modified":"2022-12-20T12:52:43","modified_gmt":"2022-12-20T04:52:43","slug":"json-%e4%bb%80%e4%b9%88%e6%98%afjson%e3%80%81json%e7%9a%84%e5%ae%9a%e4%b9%89%e5%92%8c%e8%ae%bf%e9%97%ae%e3%80%81json%e5%9c%a8javascript%e4%b8%ad%e4%b8%a4%e7%a7%8d%e5%b8%b8%e7%94%a8%e7%9a%84%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=2731","title":{"rendered":"JSON-\u4ec0\u4e48\u662fJSON\u3001JSON\u7684\u5b9a\u4e49\u548c\u8bbf\u95ee\u3001JSON\u5728JavaScript\u4e2d\u4e24\u79cd\u5e38\u7528\u7684\u8f6c\u6362\u65b9\u6cd5\u3001JavaBean\u548cjson\u7684\u76f8\u4e92\u8f6c\u6362List\u96c6\u5408\u548cjson\u7684\u76f8\u4e92\u8f6c\u6362\u3001Map\u96c6\u5408\u548cjson\u7684\u76f8\u4e92\u8f6c\u6362"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1\u3001\u4ec0\u4e48\u662f JSON?<\/h2>\n\n\n\n<p>JSON (JavaScript Object Notation) \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\u3002\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002JSON<br>\u91c7\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684\u6587\u672c\u683c\u5f0f\uff0c\u800c\u4e14\u5f88\u591a\u8bed\u8a00\u90fd\u63d0\u4f9b\u4e86\u5bf9 json \u7684\u652f\u6301\uff08\u5305\u62ec C, C++, C#, Java, JavaScript, Perl, Python<br>\u7b49\uff09\u3002 \u8fd9\u6837\u5c31\u4f7f\u5f97 JSON \u6210\u4e3a\u7406\u60f3\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002<\/p>\n\n\n\n<p>json \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002<\/p>\n\n\n\n<p>\u8f7b\u91cf\u7ea7\u6307\u7684\u662f\u8ddf xml \u505a\u6bd4\u8f83\u3002<\/p>\n\n\n\n<p>\u6570\u636e\u4ea4\u6362\u6307\u7684\u662f\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4e1a\u52a1\u6570\u636e\u7684\u4f20\u9012\u683c\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1.1\u3001json \u7684\u5b9a\u4e49<\/h3>\n\n\n\n<p>json \u662f\u7531\u952e\u503c\u5bf9\u7ec4\u6210\uff0c\u5e76\u4e14\u7531\u82b1\u62ec\u53f7\uff08\u5927\u62ec\u53f7\uff09\u5305\u56f4\u3002\u6bcf\u4e2a\u952e\u7531\u5f15\u53f7\u5f15\u8d77\u6765\uff0c\u952e\u548c\u503c\u4e4b\u95f4\u4f7f\u7528\u5192\u53f7\u8fdb\u884c\u5206\u9694\uff0c\u591a\u7ec4\u952e\u503c\u5bf9\u4e4b\u95f4\u8fdb\u884c\u9017\u53f7\u8fdb\u884c\u5206\u9694\u3002<\/p>\n\n\n\n<p>json \u5b9a\u4e49\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ json\u7684\u5b9a\u4e49\r\n\t\t\tvar jsonObj = {\r\n\t\t\t\t\"key1\":12,\r\n\t\t\t\t\"key2\":\"abc\",\r\n\t\t\t\t\"key3\":true,\r\n\t\t\t\t\"key4\":&#91;11,\"arr\",false],\r\n\t\t\t\t\"key5\":{\r\n\t\t\t\t\t\"key5_1\" : 551,\r\n\t\t\t\t\t\"key5_2\" : \"key5_2_value\"\r\n\t\t\t\t},\r\n\t\t\t\t\"key6\":&#91;{\r\n\t\t\t\t\t\"key6_1_1\":6611,\r\n\t\t\t\t\t\"key6_1_2\":\"key6_1_2_value\"\r\n\t\t\t\t},{\r\n\t\t\t\t\t\"key6_2_1\":6621,\r\n\t\t\t\t\t\"key6_2_2\":\"key6_2_2_value\"\r\n\t\t\t\t}]\r\n\t\t\t};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.1.2\u3001json \u7684\u8bbf\u95ee<\/h3>\n\n\n\n<p>json \u672c\u8eab\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>json \u4e2d\u7684 key \u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2a\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>json \u4e2d\u7684 key \u8bbf\u95ee\u5c31\u8ddf\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u4e00\u6837\uff1a json \u5bf9\u8c61.key<\/p>\n\n\n\n<p>json \u8bbf\u95ee\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alert(typeof(jsonObj));\/\/ object json\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\r\n\t\t\talert(jsonObj.key1); \/\/ 12\r\n\t\t\talert(jsonObj.key2); \/\/ abc\r\n\t\t\talert(jsonObj.key3); \/\/ true\r\n\r\n\t\t\talert(jsonObj.key4); \/\/ \u5f97\u5230\u6570\u7ec4&#91;11,\"arr\",false]\r\n\r\n\t\t\tfor (var i = 0; i &lt; jsonObj.key4.length; i++) {\r\n\t\t\t\talert(jsonObj.key4&#91;i]);\r\n\t\t\t}\r\n\r\n\t\t\talert(jsonObj.key5.key5_1); \/\/551\r\n\t\t\talert(jsonObj.key5.key5_2); \/\/key5_2_value\r\n\r\n\t\t\talert(jsonObj.key6) \/\/ \u5f97\u5230json\u6570\u7ec4\r\n\r\n\t\t\t\/\/ \u53d6\u51fa\u6765\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u662fjson\u5bf9\u8c61\r\n\t\t\tvar jsonItem = jsonObj.key6&#91;0];\r\n\t\t\talert(jsonItem.key6_1_1); \/\/6611\r\n\t\t\talert(jsonItem.key6_1_2); \/\/key6_1_2_value\r\n\r\n\t\t\talert(jsonObj);\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.1.3\u3001json \u7684\u4e24\u4e2a\u5e38\u7528\u65b9\u6cd5<\/h3>\n\n\n\n<p>json \u7684\u5b58\u5728\u6709\u4e24\u79cd\u5f62\u5f0f\u3002<br>\u4e00\u79cd\u662f\uff1a\u5bf9\u8c61\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u6211\u4eec\u53eb\u5b83 json \u5bf9\u8c61\u3002<br>\u4e00\u79cd\u662f\uff1a\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u6211\u4eec\u53eb\u5b83 json \u5b57\u7b26\u4e32\u3002<br>\u4e00\u822c\u6211\u4eec\u8981\u64cd\u4f5c json \u4e2d\u7684\u6570\u636e\u7684\u65f6\u5019\uff0c\u9700\u8981 json \u5bf9\u8c61\u7684\u683c\u5f0f\u3002<br>\u4e00\u822c\u6211\u4eec\u8981\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u7684\u65f6\u5019\uff0c\u4f7f\u7528 json \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>JSON.stringify() \u628a json \u5bf9\u8c61\u8f6c\u6362\u6210\u4e3a json \u5b57\u7b26\u4e32<\/p>\n\n\n\n<p>JSON.parse() \u628a json \u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e3a json \u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\t\t\/\/ \u628ajson\u5bf9\u8c61\u8f6c\u6362\u6210\u4e3a json\u5b57\u7b26\u4e32\r\n\t\t\tvar jsonObjString = JSON.stringify(jsonObj); \/\/ \u7279\u522b\u50cf Java\u4e2d\u5bf9\u8c61\u7684toString\r\n\t\t\talert(jsonObjString);\r\n\t\t\t\/\/ \u628ajson\u5b57\u7b26\u4e32\u3002\u8f6c\u6362\u6210\u4e3ajson\u5bf9\u8c61\r\n\t\t\tvar jsonObj2 = JSON.parse(jsonObjString);\r\n\t\t\talert(jsonObj2.key1);\/\/ 12\r\n\t\t\talert(jsonObj2.key2);\/\/ abc<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1.2\u3001JSON \u5728 java \u4e2d\u7684\u4f7f\u7528<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.2.1\u3001javaBean \u548c json \u7684\u4e92\u8f6c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>    @Test\r\n    public void test1(){\r\n        Person person = new Person(1,\"\u6211\u597d\u5e05\uff01\");\r\n        \/\/ \u521b\u5efa Gson \u5bf9\u8c61\u5b9e\u4f8b\r\n        Gson gson = new Gson();\r\n        \/\/ toJson\u65b9\u6cd5\u53ef\u4ee5\u628ajava\u5bf9\u8c61\u8f6c\u6362\u6210\u4e3ajson\u5b57\u7b26\u4e32\r\n        String personJsonString = gson.toJson(person);\r\n        System.out.println(personJsonString);\r\n        \/\/ fromJson\u628ajson\u5b57\u7b26\u4e32\u8f6c\u6362\u56deJava\u5bf9\u8c61\r\n        \/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\u662fjson\u5b57\u7b26\u4e32\r\n        \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8f6c\u6362\u56de\u53bb\u7684Java\u5bf9\u8c61\u7c7b\u578b\r\n        Person person1 = gson.fromJson(personJsonString, Person.class);\r\n        System.out.println(person1);\r\n\r\n    }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.2.2\u3001List \u548c json \u7684\u4e92\u8f6c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Test\r\n    public void test2(){\r\n        List&lt;Person> personList = new ArrayList&lt;>();\r\n\r\n        personList.add(new Person(1,\"\u6211\"));\r\n        personList.add(new Person(2,\"\u4f60\"));\r\n\r\n        Gson gson = new Gson();\r\n        \/\/ \u628aList\u8f6c\u6362\u4e3aJson\u5b57\u7b26\u4e32\r\n        String personListJsonString = gson.toJson(personList);\r\n        System.out.println(personListJsonString);\r\n\r\n        List&lt;Person> list = gson.fromJson(personListJsonString, new PersonListType().getType());\r\n        System.out.println(list);\r\n        Person person = list.get(0);\r\n        System.out.println(person);\r\n    }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.2.3\u3001map \u548c json \u7684\u4e92\u8f6c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public void test3(){\r\n        Map&lt;Integer,Person> personMap = new HashMap&lt;>();\r\n\r\n        personMap.put(1, new Person(1,\"\u6211\"));\r\n        personMap.put(2, new Person(2,\"\u4f60\"));\r\n\r\n        Gson gson = new Gson();\r\n        \/\/ \u628a map \u96c6\u5408\u8f6c\u6362\u6210\u4e3a json\u5b57\u7b26\u4e32\r\n        String personMapJsonString = gson.toJson(personMap);\r\n\r\n        \/\/Map&lt;Integer,Person> personMap2 = gson.fromJson(personMapJsonString, new PersonMapType().getType());\r\n        Map&lt;Integer,Person> personMap2 = gson.fromJson(personMapJsonString, new TypeToken&lt;HashMap&lt;Integer,Person>>(){}.getType());\r\n\r\n        System.out.println(personMap2);\r\n        Person p = personMap2.get(1);\r\n        System.out.println(p);\r\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4ec0\u4e48\u662f JSON? JSON (JavaScript Object Notation) \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[61],"class_list":["post-2731","post","type-post","status-publish","format-standard","hentry","category-javaweb-code","tag-javaweb"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2731"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2731"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2731\/revisions"}],"predecessor-version":[{"id":2732,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2731\/revisions\/2732"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}