I try to create a desired json structure in java but I get only last value in json structure since i creating json outside for loop. if i create it in nested for loop then its not give me desired structure of json.
My code is here:-
public static void main(String[] args) {
JSONObject TP1 = new JSONObject();
String[] alias = {"topping","cake"};
String[] entityType = {"Topping","cake"};
String[] textString = {"pizza","pancake"};
String[] usersays_text = {"I want ","I want "};
for(String usy:usersays_text)
{
TP1.put("text",usy.toString());
}
JSONObject TP2 = new JSONObject();
for(String tS:textString)
{
TP2.put("text",tS.toString());
}
for(String eT:entityType)
{
TP2.put("entityType",eT.toString());
}
for(String al:alias)
{
TP2.put("alias",al.toString());
}
JSONArray JSA=new JSONArray();
JSA.put(TP1);
JSA.put(TP2);
JSONObject root1= new JSONObject();
root1.put("parts", JSA);
JSONArray JSA4=new JSONArray();
JSA4.put(root1);
JSONObject root3= new JSONObject();
root3.put("TP", JSA4);
//To print
JSONObject json = new JSONObject(root3.toString()); // Convert text to object
System.out.println(json.toString(4));
}
which results me following json structure :-
{
"TP": [{
"parts": [{
"text": "I want "
},
{
"entityType": "cake",
"alias": "cake",
"text": "pancake"
}
]
}]
}
Desired structure for each value of string array - for ex:
{
"TP": [
{
"parts": [
{
"text": "I want "
},
{
"alias": "topping",
"text": "pizza",
"entityType": "Topping"
}
]
},
{
"parts": [
{
"text": "I want "
},
{
"alias": "cake",
"entityType": "cake",
"text": "pancake"
}
]
}
]
}
You have to use single loop, Assume each array has same length , Following code could help you
public static void main(String[] args) throws Exception {
String[] alias = {"topping", "cake"};
String[] entityType = {"Topping", "cake"};
String[] textString = {"pizza", "pancake"};
String[] usersays_text = {"I want ", "I want "};
JSONArray parts = new JSONArray();
for (int i = 0; i < usersays_text.length; i++) {
JSONArray JSA = new JSONArray();
JSONObject TP1 = new JSONObject();
TP1.put("text", usersays_text[i]);
JSONObject TP2 = new JSONObject();
TP2.put("text", textString[i]);
TP2.put("entityType", entityType[i]);
TP2.put("alias", alias[i]);
JSA.put(TP1);
JSA.put(TP2);
parts.put( JSA);
}
JSONObject partsObject = new JSONObject();
partsObject.put("parts",parts);
JSONObject root= new JSONObject();
root.put("trainingPhrases", partsObject);
//To print
JSONObject json = new JSONObject(root.toString()); // Convert text to object
System.out.println(json.toString(4));
}
Your jsonobject construction is wrong. In 'jsonobject' key is in unique ,
when you try it like this
for(String usy:usersays_text)
{
TP1.put("text",usy.toString());
}
the same key is present in the json object and values get replaced .
Please try the below code, it constructs the json object as expected.
public static void main(String args[]) {
JSONObject TP1 = new JSONObject();
String[] alias = {"topping","cake"};
String[] entityType = {"Topping","cake"};
String[] textString = {"pizza","pancake"};
String[] usersays_text = {"I want ","I want "};
JSONObject jobj = new JSONObject();
JSONArray jarr = new JSONArray();
for(int index = 0; index < usersays_text.length; index++)
{
JSONObject parts = new JSONObject();
JSONArray partsArr = new JSONArray();
JSONObject partsObj = new JSONObject();
partsObj.put("text", usersays_text[index].toString());
JSONObject cont = new JSONObject();
cont.put("alias", alias[index].toString());
cont.put("text", textString[index].toString());
cont.put("entityType", entityType[index].toString());
partsArr.put(partsObj);
partsArr.put(cont);
parts.put("parts", partsArr);
jarr.put(parts);
}
jobj.put("trainingPhrases", jarr);
System.out.println(jobj.toString(4));
}
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.