I am new to Java and below is the requirement, I have a JSONArray like:
[{"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:
{
"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:
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:
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 array = new JSONArray()
JSONObject object = array.getJSONObject(0)
You can just iterate over your JSONArray
and create JSONObject
s
UPDATE
Add this to pom.xml
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20201115</version>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.