<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      spark 國內愛好者

      spark 愛好者

      博客園 首頁 新隨筆 聯系 訂閱 管理

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

      1. JsonSlurper

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

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

      import groovy.json.JsonSlurper

      class ParseJson_01 {

          static main(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 {

       

          static main(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 numberobjecttruefalsenull. 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.

      posted on 2015-04-09 19:45  spark‘s  閱讀(6861)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 人妻无码av中文系列久| 男女性杂交内射女bbwxz| 九九热免费在线视频观看| 精品乱码一区二区三四五区| 果冻传媒mv免费播放在线观看| 国产成人亚洲精品成人区| 99热精品毛片全部国产无缓冲| 贺兰县| 日韩精品无码去免费专区| 欧美极品色午夜在线视频| 亚洲熟少妇一区二区三区| 国产成人高清精品亚洲| 亚洲熟妇久久精品| 日本肉体xxxx裸交| 最新亚洲人成网站在线影院 | 国产精品亚洲А∨天堂免| 亚洲欧美日韩综合一区二区 | 精品无码久久久久国产动漫3d| 亚洲色成人一区二区三区人人澡人人妻人人爽人人蜜桃麻豆 | 日本一区二区三本视频在线观看| 国产成人午夜在线视频极速观看| 男女性杂交内射女bbwxz| 日韩精品一区二区高清视频| 亚洲成人四虎在线播放| 南昌县| 少妇粗大进出白浆嘿嘿视频| 免费99视频| 无码人妻丝袜在线视频| 无套内谢少妇一二三四| 欧洲女人牲交性开放视频| 亚洲精品自拍视频在线看| 国产精品中文字幕在线| 亚洲欧美综合在线天堂| 亚洲中文字幕有综合久久| 国产欧美日韩精品丝袜高跟鞋| 成人伊人青草久久综合网| 亚洲日本精品一区二区| 日韩国产中文字幕精品| 一区二区三区四区国产综合| 九九热精彩视频在线免费| 伊人色综合久久天天|