Ask Your Question
0

How can a list of objects in Groovy be created from Json?

asked 2021-10-31 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-04-12 18:00:00 +0000

plato gravatar image

To create a list of objects in Groovy from Json, you can use the JsonSlurper class. Here is an example code:

import groovy.json.JsonSlurper

def jsonString = '''[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]'''

def jsonSlurper = new JsonSlurper()
List<Map<String,Object>> objectList = jsonSlurper.parseText(jsonString)

println objectList

In this example, we use the JsonSlurper class to parse the Json string into a list of maps (List<Map<String,Object>>). Each map represents an object in the Json array. We then print the list for verification.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-31 11:00:00 +0000

Seen: 12 times

Last updated: Apr 12 '21