jsonbuilder

How to use groovy builder to generate an array-type json?

放肆的年华 提交于 2019-11-30 11:46:58
We can generate an object-type json by groovy's json builder: def builder = new groovy.json.JsonBuilder() def root = builder.people { person { firstName 'Guillame' lastName 'Laforge' // Named arguments are valid values for objects too address( city: 'Paris', country: 'France', zip: 12345, ) married true // a list of values conferences 'JavaOne', 'Gr8conf' } } def jsonStr = builder.toString() I like this type of syntax, but how to build an array-type json? E.g. [ {"code": "111", "value":"222"}, {"code": "222", "value":"444"} ] I found some documents which say we should use JsonBuilder()

How to use groovy builder to generate an array-type json?

旧街凉风 提交于 2019-11-29 17:49:19
问题 We can generate an object-type json by groovy's json builder: def builder = new groovy.json.JsonBuilder() def root = builder.people { person { firstName 'Guillame' lastName 'Laforge' // Named arguments are valid values for objects too address( city: 'Paris', country: 'France', zip: 12345, ) married true // a list of values conferences 'JavaOne', 'Gr8conf' } } def jsonStr = builder.toString() I like this type of syntax, but how to build an array-type json? E.g. [ {"code": "111", "value":"222"}

Groovy Simple JSON array builder

爷,独闯天下 提交于 2019-11-29 14:05:43
I need to build a simple JSON array in JSON but in the loop it overwrites the first value during every iteration. def jsonBuilder = new groovy.json.JsonBuilder() contact.each { jsonBuilder.contact( FirstName: it.getFirstName(), LastName: it.getLastName(), Title: it.getTitle(), ) } It returns just the simple JSON and overwrites the value of every iteration and retains just the last one. What is the syntax for constructing a JSON Array in groovy? Trick is to collect from the list of contacts. Assuming structure of contract list is as below, follow the way jsonBuilder is used below. def contact =

Groovy Simple JSON array builder

走远了吗. 提交于 2019-11-28 07:24:44
问题 I need to build a simple JSON array in JSON but in the loop it overwrites the first value during every iteration. def jsonBuilder = new groovy.json.JsonBuilder() contact.each { jsonBuilder.contact( FirstName: it.getFirstName(), LastName: it.getLastName(), Title: it.getTitle(), ) } It returns just the simple JSON and overwrites the value of every iteration and retains just the last one. What is the syntax for constructing a JSON Array in groovy? 回答1: Trick is to collect from the list of