[英]Extract elements from List of Strings and assign it to a separate String
我是 Java 的新手,下面是要求,我有一个像这样的 JSONArray:
[{"candidate_id":"agent_4","cluster_ID":"To_Be_Added","count":"2","utterance":"Can I text you a confirmation code on the line ending in 1544"}, {"candidate_id":"agent_11","cluster_ID":"To_Be_Added","count":"2","utterance":"Can I text you a confirmation code on the line ending in 3544"}, {"candidate_id":"agent_16","cluster_ID":"To_Be_Added","count":"63","utterance":"Hello Janet can you confirm your phone number?"}
要求是从这个 JSONArray 中一个一个地提取键并分配给一个 JSONObject,例如:
{
"candidate_id": "agent_11",
"cluster_ID":"To_Be_Added",
"count":"63",
"utterance":"Can I text you a confirmation code on the line ending in 1544"
} and the rest as well as separate
现在,我将这个 JSONArray 转换为字符串列表,我将在其中逐一遍历并提取它:
for(int j=0;j<s.length(); j++) {
if(s.contains("candidate_id")){
final String candidate_id =exampleList.get(j);
logger.info("print candidate_id={}", candidate_id);
}
但它没有提取所需的结果,这里可以做什么?
最终的 Output 应该是这样的:
Recommendation :{
{candidate_id: "agent_11",
cluster_ID":"To_Be_Added",
count":"63",
"utterance":"Can I text you a confirmation code on the line ending in 1544"
},
{candidate_id: "agent_13",
cluster_ID":"To_Be_Added",
count":"45",
"utterance":"Can I text you a confirmation code on the line ending in 1544"
}}
我相信您在JSONArray
中有一个getJSONObject()
方法
JSONArray array = new JSONArray()
JSONObject object = array.getJSONObject(0)
您可以遍历JSONArray
并创建JSONObject
s
更新
将此添加到pom.xml
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20201115</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.