[英]Trouble reading json output using groovy
I have a json output that I have been able to parse successfully but I'm not able to read some of the properties value using groovy 我有一个能够成功解析的json输出,但是我无法使用groovy读取某些属性值
From the below json data, I can parse this data 从下面的json数据中,我可以解析此数据
def parsedJsonGet = new groovy.json.JsonSlurper().parseText(Response)
def i = -1
parsedJsonGet.each {
thisRecord ->
i= i+1
//Here using thisRecord can go through each node
}
From the below json data I would like to read say Street value of each such nodes. 从下面的json数据中,我想读取每个此类节点的Street值。
Json Code is in this format: Json Code的格式如下:
{
[
"Name": "ABC",
"Address": {
"":0,
"City": [
{
"Street": "Data1",
"Apt": "Data2",
"Pin": "Data3",
}
]
}
},
{
[
"Name": "ABC",
"Address": {
"":0,
"City": [
{
"Street": "",
"Apt": "",
"Pin": "",
}
]
}
}
Making a few assumptions about your payload, which isn't well-formed JSON, you could parse the data this way: 对有效载荷(不是格式正确的JSON)进行一些假设,您可以通过以下方式解析数据:
def Response = '''{
"Records": [
{
"Name": "ABC",
"Address": {
"Number": 0,
"City": [
{
"Street": "Data1",
"Apt": "Data2",
"Pin": "Data3"
}
]
}
}
]
} '''
def parsedJsonGet = new groovy.json.JsonSlurper().parseText(Response)
parsedJsonGet.Records.each {
thisRecord -> System.out.println('Street is ' + thisRecord.Address.City[0].Street)
}
Because City
is an array, I'm assuming you want just the first City/Street
record. 因为City
是一个数组,所以我假设您只需要第一个City/Street
记录。 This will return: This record is Data1
这将返回: This record is Data1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.