gpt4 book ai didi

java - 映射字符串数组列表获取键和值

转载 作者:行者123 更新时间:2023-12-02 08:47:10 26 4
gpt4 key购买 nike

根据 API 响应,我正在解析 JSON

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

下面是 parsedBlio 的输出

{"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")

您可以按如下方式获取 map 中的键和值:

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com