{"id":2237,"date":"2023-05-07T06:38:40","date_gmt":"2023-05-06T22:38:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2237"},"modified":"2023-05-07T09:13:44","modified_gmt":"2023-05-07T01:13:44","slug":"groovy-code-example-json","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/07\/groovy-code-example-json\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; JSON"},"content":{"rendered":"<h2>JSON\u89e3\u6790\u4e0e\u751f\u6210<\/h2>\n<p>groovy\u652f\u6301json\u7684\u89e3\u6790\u4e0e\u751f\u6210\uff0c\u4f7f\u7528<code>groovy.json<\/code>\u5305\u63d0\u4f9b\u7684\u529f\u80fd\u5f88\u5bb9\u6613\u5b9e\u73b0groovy\u7c7b\u4e0ejson\u4e4b\u95f4\u7684\u6620\u5c04\u3002<\/p>\n<h3>JsonSlurper<\/h3>\n<p>JsonSlurper \u662f\u7528\u4e8e\u89e3\u6790JSON\u6570\u636e\u6216\u5c06\u5176\u8f6c\u6362\u4e3agroovy\u7684\u5bf9\u8c61\uff0c\u5982\uff1amap\u3001list\u3001String\u3001integer\u7b49\u3002<\/p>\n<p>\u8be5\u7c7b\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684parse\u591a\u6001\u65b9\u6cd5\u8fd8\u6709parseText,parseFile\u7b49\u7279\u6b8a\u65b9\u6cd5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5c06\u8bb2\u89e3parseText\u65b9\u6cd5\u7684\u4f7f\u7528\u3002\u8be5\u65b9\u6cd5\u89e3\u6790JSON\u5b57\u7b26\u4e32\u5e76\u9012\u5f52\u7684\u5c06\u5176\u8f6c\u6362\u6210\u4e3a\u5bf9\u8c61\u5217\u8868\u3001\u5bf9\u8c61map\u3002\u5176\u4ed6\u7684parse*\u65b9\u6cd5\u529f\u80fd\u4e0a\u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u53ea\u662f\u53c2\u6570\u7c7b\u578b\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<pre><code class=\"language-java\">def jsonSlurper = new JsonSlurper()\ndef object = jsonSlurper.parseText(&#039;{ &quot;name&quot;: &quot;John Doe&quot; } \/* some comment *\/&#039;)\n\nassert object instanceof Map\nassert object.name == &#039;John Doe&#039;<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06json\u8f6c\u6362\u4e3amap\uff0cJsonSlurper\u652f\u6301\u6ce8\u91ca\u3002<\/p>\n<p>\u5bf9\u4e8eJSON\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u4f1a\u88ab\u8f6c\u6362\u6210\u4e3agroovy\u4e2d\u7684List\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-java\">def jsonSlurper = new JsonSlurper()\ndef object = jsonSlurper.parseText(&#039;{ &quot;myList&quot;: [4, 8, 15, 16, 23, 42] }&#039;)\n\nassert object instanceof Map\nassert object.myList instanceof List\nassert object.myList == [4, 8, 15, 16, 23, 42]<\/code><\/pre>\n<p>JSON\u652f\u6301\u4ee5\u4e0b\u6570\u636e\u7c7b\u578b\uff1astring,number,object,true,false,null\u3002JsonSlurper\u4f1a\u5c06JSON\u6570\u636e\u7c7b\u578b\u6620\u5c04\u5230\u76f8\u5bf9\u5e94\u7684groovy\u7c7b\u578b\u3002<\/p>\n<pre><code class=\"language-java\">def jsonSlurper = new JsonSlurper()\ndef object = jsonSlurper.parseText &#039;&#039;&#039;\n    { &quot;simple&quot;: 123,\n      &quot;fraction&quot;: 123.66,\n      &quot;exponential&quot;: 123e12\n    }&#039;&#039;&#039;\n\nassert object instanceof Map\nassert object.simple.class == Integer\nassert object.fraction.class == BigDecimal\nassert object.exponential.class == BigDecimal<\/code><\/pre>\n<p>JSON\u4e0eGroovy\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u5e94\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">JSON<\/th>\n<th style=\"text-align: left;\">Groovy<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">String<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">number<\/td>\n<td style=\"text-align: left;\">BigDecimal\/Integer<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">object<\/td>\n<td style=\"text-align: left;\">LinkedHashMap<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">array<\/td>\n<td style=\"text-align: left;\">ArrayList<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">true<\/td>\n<td style=\"text-align: left;\">true<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">false<\/td>\n<td style=\"text-align: left;\">false<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">null<\/td>\n<td style=\"text-align: left;\">null<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">date<\/td>\n<td style=\"text-align: left;\">java.util.Date based on the yyyy-MM-dd&#8217;T&#8217;HH:mm:ssZ date format<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>JSON\u89e3\u6790\u7c7b\u7684\u5176\u4ed6\u5b9e\u73b0<\/h2>\n<p>JsonSlurper\u6709\u4e00\u7cfb\u5217\u7684\u89e3\u6790\u5b9e\u73b0\u3002\u6bcf\u79cd\u5b9e\u73b0\u5bf9\u5e94\u7740\u4e0d\u540c\u7684\u89e3\u6790\u9700\u6c42\uff0c\u9ed8\u8ba4\u5b9e\u73b0\u53ef\u80fd\u5bf9\u4e8e\u5927\u591a\u6570\u573a\u666f\u662f\u5408\u9002\u7684\uff0c\u4f46\u4e0d\u662f\u5bf9\u4e8e\u6240\u6709\u573a\u666f\u90fd\u662f\u6700\u4f73\u7684\u3002\u4ee5\u4e0b\u662fJsonSlurper\u89e3\u6790\u7c7b\u7684\u5176\u4ed6\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li>JsonParserCharArray \u7528\u6237\u89e3\u6790JSON\u5b57\u7b26\u4e32\u3002\u4e3b\u8981\u9488\u5bf9\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\u7ec4\u64cd\u4f5c\u3002\u5728\u8f6c\u5316\u8fc7\u7a0b\u4e2d\uff0c\u901a\u8fc7\u62f7\u8d1d\u76f8\u5bf9\u5e94\u90e8\u5206\u7684\u5b57\u7b26\u5b50\u6570\u7ec4\u5e76\u5bf9\u5176\u8fdb\u884c\u7c7b\u578b\u8f6c\u5316\u7b49\u64cd\u4f5c\u5b8c\u6210JSON\u7684\u89e3\u6790\u3002<\/li>\n<li>JsonFastParser\u662f\u4e00\u4e2a\u6bd4\u8f83\u7279\u6b8a\u7684JsonParserCharArray\u89e3\u6790\u5b9e\u73b0\uff0c\u662f\u6700\u5feb\u7684\u89e3\u6790\u65b9\u5f0f\u3002\u4f46\u662f\uff0c\u4ed6\u4e0d\u662f\u9ed8\u8ba4\u7684\u89e3\u6790\u65b9\u5f0f\u3002JsonFastParser \u4e5f\u662f\u88ab\u79f0\u4e3a\u201c\u7d22\u5f15\u8986\u76d6\u89e3\u6790\u5668\u201d\u3002\u5728JSON\u89e3\u6790\u8fc7\u7a0b\u4e2d\uff0c\u89e3\u6790\u5668\u5c3d\u53ef\u80fd\u7684\u907f\u514d\u521b\u5efa\u65b0\u7684\u5b57\u7b26\u6570\u7ec4\u6216\u8005\u5b57\u7b26\u4e32\u3002\u603b\u662f\u6307\u5411\u5df2\u6709\u7684\u5b57\u7b26\u6570\u636e\u3002\u6b64\u5916\uff0c\u8be5\u65b9\u5f0f\u603b\u662f\u5c3d\u53ef\u80fd\u8fdf\u7684\u9500\u6bc1\u5bf9\u8c61\u3002\u5982\u679c\u88ab\u89e3\u6790\u7684map\u88ab\u653e\u7f6e\u5230\u957f\u671f\u7f13\u5b58\u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e2amap\u5bf9\u8c61\u5fc5\u987b\u4e0d\u4f1a\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u8fd8\u987b\u6307\u5411\u539f\u5148\u7684\u5b57\u7b26\u7f13\u5b58\u3002<\/li>\n<li>JsonParserLax\u4e5f\u662f\u4e00\u4e2a\u6bd4\u8f83\u7279\u6b8a\u7684JsonParserCharArray\u89e3\u6790\u5b9e\u73b0\u3002\u5b83\u7684\u6027\u80fd\u4e0eJsonFastParser\u76f8\u5f53\u3002\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u4e0d\u4f9d\u8d56\u4e8eECMA-404 JSON \u6807\u51c6\u3002\u6bd4\u5982\uff0c\u5b83\u5141\u8bb8json\u4e2d\u51fa\u73b0\u6ce8\u91ca\u548c\u6ca1\u6709\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u7b49\u3002<\/li>\n<li>JsonParserUsingCharacterSource\u662f\u9002\u7528\u4e8e\u5927\u6587\u4ef6\u7684JSON\u89e3\u6790\u5668\u3002\u5b83\u4f7f\u7528\u4e86 &quot;character windowing&quot; \u6280\u672f\u89e3\u6790JSON\u5927\u6587\u4ef6 \uff08\u5927\u6587\u4ef6\u610f\u5473\u7740\u81f3\u5c112M\u4ee5\u4e0a\uff09\u4ece\u800c\u6709\u7740\u6bd4\u8f83\u7a33\u5b9a\u7684\u89e3\u6790\u6027\u80fd\u3002<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5b9e\u73b0\u7c7b<\/th>\n<th style=\"text-align: left;\">\u5bf9\u5e94\u5e38\u91cf<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">JsonParserCharArray<\/td>\n<td style=\"text-align: left;\">JsonParserType#CHAR_BUFFER<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">JsonFastParser<\/td>\n<td style=\"text-align: left;\">JsonParserType#INDEX_OVERLAY<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">JsonParserLax<\/td>\n<td style=\"text-align: left;\">JsonParserType#LAX<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">JsonParserUsingCharacterSource<\/td>\n<td style=\"text-align: left;\">JsonParserType#CHARACTER_SOURCE<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8981\u6539\u53d8\u89e3\u6790\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>JsonSlurper#setType()<\/code>\u8bbe\u7f6e\u89e3\u6790\u65b9\u5f0fJsonParserType<\/p>\n<pre><code class=\"language-java\">def jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY)\ndef object = jsonSlurper.parseText(&#039;{ &quot;myList&quot;: [4, 8, 15, 16, 23, 42] }&#039;)\n\nassert object instanceof Map\nassert object.myList instanceof List\nassert object.myList == [4, 8, 15, 16, 23, 42]<\/code><\/pre>\n<h2>Json\u5e8f\u5217\u5316\u4e4bJsonOutput<\/h2>\n<p>JsonOutput\u8d1f\u8d23\u5c06groovy\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3ajson\u5b57\u7b26\u4e32\uff0c\u8ddfJsonSlurper\u521a\u597d\u76f8\u53cd\u3002<\/p>\n<p>JsonOutput\u7c7b\u5177\u6709\u591a\u6001\u7684\u9759\u6001json\u5904\u7406\u65b9\u6cd5\u3002\u6bcf\u4e2a\u65b9\u6cd5\u7684\u5b9e\u73b0\u5bf9\u5e94\u4e0d\u540c\u7684\u53c2\u6570\u7c7b\u578b\u3002\u8fd9\u4e9b\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u9759\u6001\u5bfc\u5165\u7684\u65b9\u5f0f\u5bfc\u5165\u540e\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p>\u65b9\u6cd5\u7684\u6267\u884c\u7ed3\u679c\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-java\">def json = JsonOutput.toJson([name: &#039;John Doe&#039;, age: 42])\n\nassert json == &#039;{&quot;name&quot;:&quot;John Doe&quot;,&quot;age&quot;:42}&#039;<\/code><\/pre>\n<p>JsonOutput \u4e0d\u4ec5\u4ec5\u652f\u6301\u6570\u636e\u4e3b\u7c7b\u578b\uff0cmap\uff0clist\u7b49\u7c7b\u578b\u7684JSON\u5e8f\u5217\u5316\uff0c\u751a\u81f3\u53ef\u4ee5\u5e8f\u5217\u5316POGOs\uff0c\u4e5f\u5c31\u662f\u666e\u901a\u7684Groovy\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-java\">class Person { String name }\n\ndef json = JsonOutput.toJson([ new Person(name: &#039;John&#039;), new Person(name: &#039;Max&#039;) ])\n\nassert json == &#039;[{&quot;name&quot;:&quot;John&quot;},{&quot;name&quot;:&quot;Max&quot;}]&#039;<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5e8f\u5217\u5316\u7684JSON\u5b57\u7b26\u4e32\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6ca1\u6709\u5bf9\u9f50\u3002\u6240\u5e78\u7684\u662f\uff0cJsonOutput\u63d0\u4f9b\u4e86prettyPrint\u65b9\u6cd5\u5e2e\u52a9\u6211\u4eec\u5e8f\u5217\u5316\u4e3a\u66f4\u6613\u9605\u8bfb\u7684JSON\u683c\u5f0f\u3002<\/p>\n<pre><code class=\"language-java\">def json = JsonOutput.toJson([name: &#039;John Doe&#039;, age: 42])\n\nassert json == &#039;{&quot;name&quot;:&quot;John Doe&quot;,&quot;age&quot;:42}&#039;\n\nassert JsonOutput.prettyPrint(json) == &#039;&#039;&#039;\\\n{\n    &quot;name&quot;: &quot;John Doe&quot;,\n    &quot;age&quot;: 42\n}&#039;&#039;&#039;.stripIndent()<\/code><\/pre>\n<p>prettyPrint\u65b9\u6cd5\u63a5\u6536\u5b57\u7b26\u4e32\u53c2\u6570\u3002\u8be5\u65b9\u6cd5\u4e0d\u80fd\u4e0e\u5176\u4ed6JsonOutput\u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528\uff0c\u53ea\u80fd\u4f5c\u7528\u4e8e\u4efb\u610f\u7684JSON\u683c\u5f0f\u5b57\u7b26\u4e32\u3002<\/p>\n<p>Groovy\u4e2d\u8fd8\u6709\u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efaJSON\uff0c\u90a3\u5c31\u662f\u901a\u8fc7<code>JsonBuilder<\/code>\u7c7b\u6216\u8005<code>StreamingJsonBuilder<\/code>\u7c7b\u3002\u8fd9\u4e24\u4e2a\u7c7b\u90fd\u63d0\u4f9b\u4e86DSL\u8bed\u6cd5\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u56fe\uff0c\u7136\u540e\u8f6c\u6362\u4e3aJSON\u3002<\/p>\n<h2>groovy\u4e2d\u5982\u4f55\u5c06\u5bf9\u8c61\u8f6c\u5316\u4e3ajson\uff1f<\/h2>\n<p>groovy\u4e2d\u5c06\u5bf9\u8c61\u8f6c\u4e3ajson\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">import groovy.json.*\n\nclass Me {\n    String name\n}\n\ndef o = new Me( name: &#039;tim&#039; )\n\nprintln new JsonBuilder( o ).toPrettyString() \/\/\u8f6c\u4e3ajson<\/code><\/pre>\n<h2>groovy\u4e2d\u5982\u4f55\u5c06json\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5bf9\u8c61\uff1f<\/h2>\n<p>groovy1.8\u4ee5\u540e\u53ef\u4ee5\u4f7f\u7528JsonSlurper\u5c06json\u8f6c\u5316\u4e3a\u5bf9\u8c61\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">import groovy.json.JsonSlurper\n\ndef jsonObj = new JsonSlurper().parseText( &#039;{ &quot;name&quot;:&quot;Peter&quot;, &quot;age&quot;: 23}&#039; )\n\nassert jsonObj.name == &quot;Peter&quot;\nassert jsonObj.age == 23\n\/\/\u4e0b\u9762\u7684\u5c5e\u6027\u4e0d\u5b58\u5728\nassert jsonObj.gender == null<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JSON\u89e3\u6790\u4e0e\u751f\u6210 groovy\u652f\u6301json\u7684\u89e3\u6790\u4e0e\u751f\u6210\uff0c\u4f7f\u7528groovy.json\u5305\u63d0\u4f9b\u7684\u529f\u80fd\u5f88\u5bb9\u6613\u5b9e\u73b0gr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2237","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2237"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2237\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}