JSON to Groovy parser

后端 未结 5 2125
暗喜
暗喜 2020-12-25 13:14

I found many things about converting Groovy to JSON, but oddly enough, not the other way.

What is the (best) JSON to Groovy parser around there ?

5条回答
  •  死守一世寂寞
    2020-12-25 13:50

    If you are on Groovy 1.8 or later, there is a build in JsonSlurper you can use this way:

    import groovy.json.JsonSlurper
    
    //Attention: you have to use double quotes inside the json string
    def jsonObj = new JsonSlurper().parseText( '{ "name":"Peter", "age": 23}' )
    
    assert jsonObj.name == "Peter"
    assert jsonObj.age == 23
    //this won't work, because it's not defined
    assert jsonObj.gender == null
    

提交回复
热议问题