从API响应中,我正在解析JSON

String myData = jsonSlurper.parseText(responseBody)
Map parsedBiblio = jsonSlurper.parseText(myData)


以下是parsedBiblio的输出

{"Data": {"AppNumber": "15671037", "CustomerNumber": "81744", "Date": "08-07-2017", "Status": "Active"},

 "Applicants": [{"Name": "abcd Inc.", "Address": "xxx, CA (US)"}],


下面是检索Data键和相应值的代码

Map<Object, Object> innerMap = parsedBiblio.get("Data")
for (Object objectName : innerMap.keySet()) {

       println("Key: " + objectName + " Value: "+ innerMap.get(objectName) +".");
 }


让我知道如何重新检索Applicants键和相应的值,因为这是map<string,List<string>格式,所以我将以以下格式声明内部映射

Map<String, List<String>> innerMapApplicant = parsedBiblio.get("Applicants")


我收到以下错误

 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)

最佳答案

我认为您应该尝试按以下方式检索申​​请人:

List<Map<String,String>> applicantList =  = parsedBiblio.get("Applicants")


您可以按以下方式在地图中获取键和值:

  for(Map<String,String> applicantMap: applicantList)
  {
       for (Object objectName : applicantMap.keySet())
       {
          println("Key: " + objectName + " Value: "+ applicantMap.get(objectName) +".");
       }
   }

关于java - 映射字符串arraylist获取键和值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61005357/

10-09 09:36