- 함께 역인 글 : [Groovy] json 만들기
역시나 간단하니 예제로 시작
json 문자를 파싱하기 위해 JsonSlurper를 사용하면 간단하게 해결되고, map과 array 등으로 파싱해줘서 간편하게 사용할 수 있다.
- 참고
Groovy Goodness: Parse JSON with JsonSlurper
역시나 간단하니 예제로 시작
def jsonText = '''{
"html": {
"header": {
"title": "Login"
},
"body": {
"h1": "Hello world",
"div": {
"p": [
{
"label": "ID",
"input": "id"
},
{
"label": "Password",
"input": "pw"
}
],
"button": [
"Login",
"Exit"
]
}
}
}
}'''
def json = new groovy.json.JsonSlurper().parseText(jsonText)
def body = json.html.body
assert body.h1 == 'Hello world'
assert body.div.p[0] == ['input' : 'id','label' : 'ID']
assert body.div.p[1].label == 'Password'
"html": {
"header": {
"title": "Login"
},
"body": {
"h1": "Hello world",
"div": {
"p": [
{
"label": "ID",
"input": "id"
},
{
"label": "Password",
"input": "pw"
}
],
"button": [
"Login",
"Exit"
]
}
}
}
}'''
def json = new groovy.json.JsonSlurper().parseText(jsonText)
def body = json.html.body
assert body.h1 == 'Hello world'
assert body.div.p[0] == ['input' : 'id','label' : 'ID']
assert body.div.p[1].label == 'Password'
json 문자를 파싱하기 위해 JsonSlurper를 사용하면 간단하게 해결되고, map과 array 등으로 파싱해줘서 간편하게 사용할 수 있다.
- 참고
Groovy Goodness: Parse JSON with JsonSlurper







덧글