亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

Groovy 轉換JSON和生產JSON

2019-11-14 23:41:56
字體:
來源:轉載
供稿:網友
Groovy 轉換JSON和生產JSON

Groovy 類和JSON之間的相互轉換,主要在groovy.json包下面

1. JsonSlurper

JsonSlurper 這個類用于轉換JSON文本或從Groovy 數據結構中讀取內容例如map、list和一些基本的數據類型如Integer, Double, Boolean和String.

這個類有一系列重載的Parse的方法和一些指定特殊的方法,例如parseText,parseFile..下一個離職我們將以parseText使用為例,將JSON 字符串轉換為list 和map對象。其他parse開頭的方法與之類似只是參數不同而已,

import groovy.json.JsonSlurper

class ParseJson_01 {

staticmain(args) {

def jsonSlurper = new JsonSlurper()

def object = jsonSlurper.parseText('{ "name": "John Doe" } ')

assert object instanceof Map

assert object.name == 'John Doe'

}

}

JsonSlurper除了maps支持,JSON 數據轉被換成lists。

import groovy.json.JsonSlurper

class ParseJson_02 {

staticmain(args) {

def jsonSlurper = new JsonSlurper()

def object = jsonSlurper.parseText('{ "myList": [4, 8, 15, 16, 23, 42] }')

assert object instanceof Map

assert object.myList instanceof List

assert object.myList == [4, 8, 15, 16, 23, 42]

}

}

JSON支持一下的的標準的原始數據類型:string 、number、object、true、false和null. JsonSlurper把這些解析成相應的Groovy類型.

def jsonSlurper = new JsonSlurper()

def object = jsonSlurper.parseText '''

{ "simple": 123,

"fraction": 123.66,

"exponential": 123e12

}'''

assert object instanceof Map

assert object.simple.class == Integer

     assert object.fraction.class == BigDecimal

As JsonSlurper is returning pure Groovy object instances without any special JSON classes in the back, its usage is transparent. In fact, JsonSlurper results conform to GPath exPRessions. GPath is a powerful expression language that is supported by multiple slurpers for different data formats (xmlSlurper for XML being one example).

For more details please have a look at the section on GPath expressions.

Json

和groovy 對應的數據類型

JSON

Groovy

string

java.lang.String

number

java.lang.BigDecimal or java.lang.Integer

object

java.util.LinkedHashMap

array

java.util.ArrayList

true

true

false

false

null

null

date

java.util.Date based on the yyyy-MM-dd'T'HH:mm:ssZ date format

Whenever a value in JSON is null, JsonSlurper supplements it with the Groovy null value. This is in contrast to other JSON parsers that represent a null value with a library-provided singleton object.

1.1. Parser Variants

JsonSlurper comes with a couple of parser implementations. Each parser fits different requirements, it could well be that for certain scenarios the JsonSlurper default parser is not the best bet for all situations. Here is an overview of the shipped parser implementations:

  • The JsonParserCharArray parser basically takes a JSON string and Operates on the underlying character array. During value conversion it copies character sub-arrays (a mechanism known as "chopping") and operates on them.
  • The JsonFastParser is a special variant of the JsonParserCharArray and is the fastest parser. However, it is not the default parser for a reason. JsonFastParser is a so-called index-overlay parser. During parsing of the given JSON String it tries as hard as possible to avoid creating new char arrays or String instances. It keeps pointers to the underlying original character array only. In addition, it defers object creation as late as possible. If parsed maps are put into long-term caches care must be taken as the map objects might not be created and still consist of pointer to the original char buffer only. However, JsonFastParser comes with a special chop mode which dices up the char buffer early to keep a small copy of the original buffer. Recommendation is to use the JsonFastParser for JSON buffers under 2MB and keeping the long-term cache restriction in mind.
  • The JsonParserLax is a special variant of the JsonParserCharArray parser. It has similar performance characteristics as JsonFastParser but differs in that it isn't exclusively relying on the ECMA-404 JSON grammar. For example it allows for comments, no quote strings etc.
  • The JsonParserUsingCharacterSource is a special parser for very large files. It uses a technique called "character windowing" to parse large JSON files (large means files over 2MB size in this case) with constant performance characteristics.

The default parser implementation for JsonSlurper is JsonParserCharArray. The JsonParserType enumeration contains constants for the parser implementations described above:

Implementation

Constant

JsonParserCharArray

JsonParserType#CHAR_BUFFER

JsonFastParser

JsonParserType#INDEX_OVERLAY

JsonParserLax

JsonParserType#LAX

JsonParserUsingCharacterSource

JsonParserType#CHARACTER_SOURCE

Changing the parser implementation is as easy as setting the JsonParserType with a call to JsonSlurper#setType().

def jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY)
def object = jsonSlurper.parseText('{ "myList": [4, 8, 15, 16, 23, 42] }')

assert object instanceof Map
assert object.myList instanceof List
assert object.myList == [4, 8, 15, 16, 23, 42]
2. JsonOutput

JsonOutput is responsible for serialising Groovy objects into JSON strings. It can be seen as companion object to JsonSlurper, being a JSON parser.

JsonOutput comes with overloaded, static toJson methods. Each toJson implementation takes a different parameter type. The static method can either be used directly or by importing the methods with a static import statement.

The result of a toJson call is a String containing the JSON code.

def json = JsonOutput.toJson([name: 'John Doe', age: 42])

assert json == '{"name":"John Doe","age":42}'

JsonOutput does not only support primitive, maps or list data types to be serialized to JSON, it goes further and even has support for serialising POGOs, that is, plain-old Groovy objects.

class Person { String name }

def json = JsonOutput.toJson([ new Person(name: 'John'), new Person(name: 'Max') ])

assert json == '[{"name":"John"},{"name":"Max"}]'

As we saw in previous examples, the JSON output is not pretty printed per default. However, the prettyPrint method in JsonSlurper comes to rescue for this task.

def json = JsonOutput.toJson([name: 'John Doe', age: 42])

assert json == '{"name":"John Doe","age":42}'

assert JsonOutput.prettyPrint(json) == '''/
{
    "name": "John Doe",
    "age": 42
}'''.stripIndent()

prettyPrint takes a String as single parameter. It must not be used in conjunction with the other JsonOutput methods, it can be applied on arbitrary JSON String instances.

Another way to create JSON from Groovy is to use the JsonBuilder or StreamingJsonBuilder. Both builders provide a DSL which allows to formulate an object graph which is then converted to JSON at some point.

For more details on builders, have a look at the builders chapter which covers both JSON builders in great depth.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美高清在线视频观看不卡| 91精品国产免费久久久久久| 九九精品在线观看| 亚洲韩国欧洲国产日产av| 日韩欧美综合在线视频| 欧美日韩国产成人在线观看| 亚洲自拍av在线| 欧美乱大交xxxxx另类电影| 国产精品视频在线观看| 国产精品网址在线| 亚洲影影院av| 国产精品自产拍在线观| 国产精品免费一区豆花| 欧美另类在线观看| 97超碰国产精品女人人人爽| 97国产精品视频| 国产美女搞久久| 日韩在线中文字幕| 尤物99国产成人精品视频| 久久久久国产精品www| 国精产品一区一区三区有限在线| 色婷婷综合成人av| 91精品国产自产在线观看永久| 中文字幕亚洲欧美日韩2019| 日本精品视频在线| 热久久免费视频精品| 日韩欧美在线观看视频| 亚洲天堂av在线免费| 亚洲欧美制服综合另类| 欧美孕妇毛茸茸xxxx| 一区二区欧美久久| 亚洲第一区在线| 国产一区二区三区精品久久久| 影音先锋日韩有码| 日本免费久久高清视频| 九九精品在线视频| 日韩欧美aⅴ综合网站发布| 国产精品日韩久久久久| 国产成人高清激情视频在线观看| 亚洲精品一区二区三区婷婷月| 欧美日韩电影在线观看| 久久91亚洲精品中文字幕| 亚洲国产高清高潮精品美女| 这里只有精品视频在线| 国产精品成人av在线| 亚洲国产精品资源| 亚洲а∨天堂久久精品喷水| 777国产偷窥盗摄精品视频| 久久福利视频网| 黑人极品videos精品欧美裸| 色老头一区二区三区在线观看| 91美女高潮出水| 亚洲аv电影天堂网| 欧美黑人巨大xxx极品| 欧美电影《睫毛膏》| 国产精自产拍久久久久久蜜| 亚洲国产精品久久91精品| 欧美日韩高清区| 国产一区二区色| 在线观看国产精品日韩av| 亚洲免费人成在线视频观看| 蜜臀久久99精品久久久久久宅男| 欧美一级片久久久久久久| 中文字幕国产日韩| 久久综合电影一区| 亚洲影院色无极综合| 午夜精品美女自拍福到在线| 久久成人亚洲精品| 91精品在线观看视频| 精品一区二区电影| 成人激情视频小说免费下载| 久久综合免费视频影院| 欧美视频中文在线看| 欧美美女18p| 欧美日韩在线免费| 中文字幕av一区| 欧美大胆在线视频| 亚洲色图欧美制服丝袜另类第一页| 亚洲欧美资源在线| 亚洲xxx自由成熟| 日韩av影视综合网| xxav国产精品美女主播| 国产一区二区三区免费视频| 欧美激情极品视频| 欧美丝袜第一区| 97在线日本国产| 亚洲第一精品自拍| 国产亚洲精品成人av久久ww| 成人444kkkk在线观看| 国产激情久久久久| 亚洲a成v人在线观看| 俺也去精品视频在线观看| 欧美一区二区三区免费视| 91国内免费在线视频| 日韩视频欧美视频| 国产精品中文在线| 国产精品日韩在线播放| 国内精品久久久久久久| 久久久久久久久91| 久久久www成人免费精品张筱雨| 成人精品一区二区三区电影黑人| 国产精品中文久久久久久久| 国产精品久久久久久网站| 中文字幕综合在线| 琪琪第一精品导航| 激情久久av一区av二区av三区| 精品视频www| 91超碰中文字幕久久精品| 欧美成人四级hd版| 一区二区三区黄色| 亚洲精品视频免费在线观看| 欧美第一页在线| 国产精品成人观看视频国产奇米| 91在线|亚洲| 性欧美激情精品| 91精品中国老女人| 久久影视电视剧免费网站清宫辞电视| 亚洲欧美制服综合另类| 亚洲欧美另类中文字幕| 91亚洲永久免费精品| 欧美亚洲国产精品| 国产精品黄页免费高清在线观看| 国产精品h在线观看| 亚洲网站视频福利| 日本免费一区二区三区视频观看| 亚洲qvod图片区电影| 久久久久久久久久久av| 97视频免费在线看| 97在线视频精品| 国产精品高清网站| 欧美精品成人91久久久久久久| 91精品久久久久久久久久久| 国产精品爽爽爽爽爽爽在线观看| 精品国产一区二区三区四区在线观看| 91a在线视频| 亚洲精品电影网在线观看| 亚洲欧洲黄色网| 欧美一级视频一区二区| 26uuu另类亚洲欧美日本老年| 成人激情视频小说免费下载| 国产精品成人v| 欧美激情视频网站| 欧美日韩精品在线观看| 日韩亚洲精品视频| 日日狠狠久久偷偷四色综合免费| 国产精品久久久久久五月尺| 欧美日韩亚洲网| 亚洲国产精品va在线看黑人| 亚洲а∨天堂久久精品9966| 欧美激情国产高清| 欧美国产日韩一区二区三区| 国产精品白丝av嫩草影院| 欧美黄色三级网站| 国产精品视频白浆免费视频| 久久亚洲一区二区三区四区五区高| 日韩国产精品亚洲а∨天堂免| 日本一区二区在线免费播放| 亚洲美女性生活视频| 色悠悠久久久久| 久久久久久国产免费| 久久人人爽人人爽人人片av高清| 日韩欧美在线观看视频| 91精品国产综合久久久久久久久| 亚洲欧美在线一区|