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 inJSONArray
You can just iterate over your
JSONArray
and createJSONObject
sUPDATE
Add this to
pom.xml