在Groovy中Slurping JSON集合

在Groovy中Slurping JSON集合,json,groovy,deserialization,Json,Groovy,Deserialization,我有一个JSON字符串,如下所示: String json = """ { "content":{ "response":{ "body": [ { "firstName":"Jim", "lastName":"Smith" },

我有一个JSON字符串,如下所示:

String json = """
    {       
        "content":{
            "response":{
                "body": [
                    {
                        "firstName":"Jim",
                        "lastName":"Smith"
                    },
                    {
                        "firstName":"Joe",
                        "lastName":"Smith"
                    },
                    {
                        "firstName":"Jane",
                        "lastName":"Smith"
                    }
                ]
            }
        }
    }
"""
class Person {
    String firstName
    String surname
}
我有一个POJO,看起来像这样:

String json = """
    {       
        "content":{
            "response":{
                "body": [
                    {
                        "firstName":"Jim",
                        "lastName":"Smith"
                    },
                    {
                        "firstName":"Joe",
                        "lastName":"Smith"
                    },
                    {
                        "firstName":"Jane",
                        "lastName":"Smith"
                    }
                ]
            }
        }
    }
"""
class Person {
    String firstName
    String surname
}
我既不能更改给定的JSON字符串(实际上是从web服务返回的JSON),也不能更改POJO(由其他团队拥有/维护)

我想将此JSON转换为
列表

我通过
JsonSlurper
的尝试失败:

JsonSlurper slurper = new JsonSlurper()
List<Person> people = []
// The problem is I don't know how many people there will be so
// not sure how to index the slurper.
JsonSlurper slurper=new JsonSlurper()
列出人=[]
//问题是,我不知道有多少人会这样
//不知道如何索引slurper。

我认为最好的方法是迭代
slurper
,将每个person JSON对象转换为
person
实例,然后将该person添加到
人员的列表中。但是我不太熟悉
JsonSlurper
的API,也不太熟悉最好的方法是什么。

您可以通过
collect()
编辑条目来获得列表:

List<Person> l = slurper.content.response.body.collect{ new Person(firstName: it.firstName, surname: it.lastName) }
List l=slurper.content.response.body.collect{newperson(firstName:it.firstName,姓氏:it.lastName)}
e、 g:

import groovy.json.JsonSlurper
导入groovy.transform.ToString
字符串json=“”
{       
“内容”:{
“答复”:{
“正文”:[
{
“名字”:“吉姆”,
“姓氏”:“史密斯”,
},
{
“名字”:“乔”,
“姓氏”:“史密斯”,
},
{
“名字”:“简”,
“姓氏”:“史密斯”,
}
]
}
}
}
"""
@托斯特林
班主任{
字符串名
串姓
}
def slurped=new JsonSlurper().parseText(json)
List l=slurped.content.response.body.collect{new Person(firstName:it.firstName,姓氏:it.lastName)}
断言l.size()==3

好吧,不需要显式构造Person对象:

(沟槽式外壳)

只需确保
Person
type完全代表json(例如字段名相等) 这是一个棘手的部分,因为Groovy解析器有点古怪。我的意思是,如果您不将
姓氏
重命名为
lastName
,json列表将被解析,但所有三人实例的所有字段都将保持
null

如果您不喜欢动态类型的贴图,则对象更具魔力:

@ToString
class Some {
    Content content
}

@ToString
class Content {
    Response response
}

@ToString
class Response {
    Person[] body
}

cnt = slurped as Some
println cnt.dump()
将打印

<Some@62ddbd7e content=Content(Response([Person(Jim, Smith), Person(Joe, Smith), Person(Jane, Smith)]))>


注意,那里的JSON无效。您可能需要运行LAX-mode.demobiles@cfrick(+1)-复制/粘贴错误;请查看我的修订版。代码缺少
slurped
。必须是LAX slurper(未测试)或JSON必须是固定的(已测试)。我已固定JSON并添加了
slurped