[英]Extract elements from List of Strings and assign it to a separate String
I am new to Java and below is the requirement, I have a JSONArray like:我是 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?"}
The requirement is to extract keys from this JSONArray one by one and assign to to a JSONObject like:要求是从这个 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
Now, as of now I converted this JSONArray to list of Strings in which i will traverse and extract it one by one:现在,我将这个 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);
}
but it's not extracting the required results, what can be done here?但它没有提取所需的结果,这里可以做什么?
Final Output should look like:最终的 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"
}}
I believe you have a getJSONObject()
method in JSONArray
我相信您在
JSONArray
中有一个getJSONObject()
方法
JSONArray array = new JSONArray()
JSONObject object = array.getJSONObject(0)
You can just iterate over your JSONArray
and create JSONObject
s您可以遍历
JSONArray
并创建JSONObject
s
UPDATE更新
Add this to pom.xml
将此添加到
pom.xml
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20201115</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.