Groovy 使用字符串获取 json slurper 对象中的对象
Groovy get object within json slurper object with string
我正在尝试创建一个函数,在该函数中我从 JsonSlurper
传递一个 json 对象和一个包含位于原始对象中的 json 对象的字符串。如果是,则 returns true 或 false 如果满足元素计数条件。例如:
我的Json:
{
"Errors": [],
"Loans": [
{
"Applications": [
{
"id": 1,
"name": "test"
}
]
},
{
"Applications": [
{
"id": 2,
"name": "test3"
},
{
"id": 3,
"name": "test3"
}
]
}
]
}
我的方法将得到 json 数组,如下所示:
def myJson = new JsonSlurper().parseText(receivedResponse.responseBodyContent)
def result = verifyElementsCountGreaterThanEqualTo(myJson, "Loans[0].Applications[1]", 3)
有这样的图书馆可以为我做这件事吗?
我已尝试 myJson["Loans[0].Applications[1]"]
获取 Json 对象以便获取大小,但结果是 null
。
您可以尝试将您的 json 转换为 java 对象 Map
以确保准确,之后您可以获得 Loans
作为对象 ArrayList
.
def myJson = new JsonSlurper().parseText("{\"Errors\": [], \"Loans\": [{\"id\": 1}, {\"id\": 2}]}");
def loansList = myJson.Loans// ArrayList
下面的怎么样?我想这很微不足道。
Loans
是一个包含多个 Applications
的列表。只需传递应用程序的索引即可。
def json = new groovy.json.JsonSlurper().parseText(jsonString)
//Closure to get the particular Loan
def getLoanAt = { json.Loans[it]}
//Call above closure as method to print the 2nd Applications
println getLoanAt(1)
如果您想打印所有贷款申请,这里根本不需要关闭:
json.Loans.each {println it}
在线demo进行快速测试
如果您想通过 Id 申请贷款,请使用以下内容:
//To get all loan application by Id
def getApplicationById = {id -> json.Loans.Applications.flatten().find{id == it.id}}
println getApplicationById(3)
上面的快速 demo。
经过多方查找,终于找到了放心的解决方案api。
我可以使用 string
作为我在 Json 对象中寻找的路径,如下所示:
import io.restassured.path.json.JsonPath as JsonPath
def myJson = "{'Errors':[],'Loans':[{'Applications':[{'id':1,'name':'test'}]},{'Applications':[{'id':2,'name':'test3'},{'id':3,'name':'test3'}]}]}"
def applicationData = JsonPath.with(myJson).get("Loans[0].Applications[1]")
def applicationsListData = JsonPath.with(myJson).get("Loans[0].Applications")
我正在尝试创建一个函数,在该函数中我从 JsonSlurper
传递一个 json 对象和一个包含位于原始对象中的 json 对象的字符串。如果是,则 returns true 或 false 如果满足元素计数条件。例如:
我的Json:
{
"Errors": [],
"Loans": [
{
"Applications": [
{
"id": 1,
"name": "test"
}
]
},
{
"Applications": [
{
"id": 2,
"name": "test3"
},
{
"id": 3,
"name": "test3"
}
]
}
]
}
我的方法将得到 json 数组,如下所示:
def myJson = new JsonSlurper().parseText(receivedResponse.responseBodyContent)
def result = verifyElementsCountGreaterThanEqualTo(myJson, "Loans[0].Applications[1]", 3)
有这样的图书馆可以为我做这件事吗?
我已尝试 myJson["Loans[0].Applications[1]"]
获取 Json 对象以便获取大小,但结果是 null
。
您可以尝试将您的 json 转换为 java 对象 Map
以确保准确,之后您可以获得 Loans
作为对象 ArrayList
.
def myJson = new JsonSlurper().parseText("{\"Errors\": [], \"Loans\": [{\"id\": 1}, {\"id\": 2}]}");
def loansList = myJson.Loans// ArrayList
下面的怎么样?我想这很微不足道。
Loans
是一个包含多个 Applications
的列表。只需传递应用程序的索引即可。
def json = new groovy.json.JsonSlurper().parseText(jsonString)
//Closure to get the particular Loan
def getLoanAt = { json.Loans[it]}
//Call above closure as method to print the 2nd Applications
println getLoanAt(1)
如果您想打印所有贷款申请,这里根本不需要关闭:
json.Loans.each {println it}
在线demo进行快速测试
如果您想通过 Id 申请贷款,请使用以下内容:
//To get all loan application by Id
def getApplicationById = {id -> json.Loans.Applications.flatten().find{id == it.id}}
println getApplicationById(3)
上面的快速 demo。
经过多方查找,终于找到了放心的解决方案api。
我可以使用 string
作为我在 Json 对象中寻找的路径,如下所示:
import io.restassured.path.json.JsonPath as JsonPath
def myJson = "{'Errors':[],'Loans':[{'Applications':[{'id':1,'name':'test'}]},{'Applications':[{'id':2,'name':'test3'},{'id':3,'name':'test3'}]}]}"
def applicationData = JsonPath.with(myJson).get("Loans[0].Applications[1]")
def applicationsListData = JsonPath.with(myJson).get("Loans[0].Applications")