From the API response, I'm parsing the JSON
String myData = jsonSlurper.parseText(responseBody)
Map parsedBiblio = jsonSlurper.parseText(myData)
below is the output of parsedBiblio
{"Data": {"AppNumber": "15671037", "CustomerNumber": "81744", "Date": "08-07-2017", "Status": "Active"},
"Applicants": [{"Name": "abcd Inc.", "Address": "xxx, CA (US)"}],
below is the code of retrieving the Data
key and corresponding value
Map<Object, Object> innerMap = parsedBiblio.get("Data")
for (Object objectName : innerMap.keySet()) {
println("Key: " + objectName + " Value: "+ innerMap.get(objectName) +".");
}
Let me know how I can retrieve the Applicants
key and the corresponding values, because this is map<string,List<string>
format, so I will declare the innermap in the below format
Map<String, List<String>> innerMapApplicant = parsedBiblio.get("Applicants")
I get this error
Cannot cast object '[{Address=xxx, CA (US), Name=abcd Inc.}]' with class 'java.util.ArrayList' to class 'java.util.Map' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.Map(groovy.json.internal.LazyMap)
I think you should try retrieving Applicant as follow:
List<Map<String,String>> applicantList = = parsedBiblio.get("Applicants")
You can fetch key and value in the map as follow:
for(Map<String,String> applicantMap: applicantList)
{
for (Object objectName : applicantMap.keySet())
{
println("Key: " + objectName + " Value: "+ applicantMap.get(objectName) +".");
}
}
Don't understand why you double parse a String
If responseBody is an string, you can write:
def json = jsonSlurper.parseText(responseBody)
println json.Data.Applicants.Name
println json.Data.Applicants.Address
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.