簡體   English   中英

從字符串列表中提取元素並將其分配給單獨的字符串

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM