Reputation: 32331
The Data inside T3 array is being repeated for every element .
This is my program where i am trying to generate the JSON , but i see that the value of T3 array is being repeated for every element
package com;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.StringTokenizer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) throws JSONException {
Test testJson = new Test();
Map<String, LinkedList<String>> categoryitemslistMap = new LinkedHashMap<String, LinkedList<String>>();
JSONObject mainObject = new JSONObject();
JSONArray T3Array = new JSONArray();
JSONArray T2Array = new JSONArray();
LinkedList<String> T3ValuesList = new LinkedList<String>();
T3ValuesList.add("Bottled");
T3ValuesList.add("Fountain");
categoryitemslistMap.put("Soft Drinks", T3ValuesList);
String t2consildated_Data = "Lemon,Orange";
for (Map.Entry<String, LinkedList<String>> entry : categoryitemslistMap.entrySet()) {
String t1data = entry.getKey();
if (t1data.equalsIgnoreCase("Soft Drinks")) {
LinkedList<String> t1ChildList = entry.getValue();
for (String t2Data : t1ChildList) {
if (t2consildated_Data != null&& !t2consildated_Data.isEmpty()) {
StringTokenizer stt = new StringTokenizer(t2consildated_Data, ",");
while (stt.hasMoreTokens()) {
String t3data = stt.nextToken();
JSONArray leaf = testJson.createLeaf();
JSONObject lemon = new JSONObject().put("name", t3data).put("leaf", new JSONArray().put(leaf));
T3Array.put(lemon);
}
}
JSONObject bottled = new JSONObject().put("name", t2Data).put("T3", T3Array);
T2Array.put(bottled);
}
JSONObject softDrink = new JSONObject().put("T2", T2Array);
JSONObject json = new JSONObject().put(t1data, softDrink);
mainObject.put("items", json);
} // end of processing values of categoryitemslistMap(Linked List)
} // end of processing categoryitemslistMap
System.out.println(mainObject);
} // end of main method
public JSONArray createLeaf() throws JSONException {
JSONArray ja = new JSONArray();
for(int i=0;i<2;i++)
{
if(i==0)
{
JSONObject jo = new JSONObject();
jo.put("name", "500 ML");
ja.put(jo);
}
else if(i==1)
{
JSONObject jo = new JSONObject();
jo.put("name", "1 Litre");
ja.put(jo);
}
}
return ja;
}
}
This is the JSON it is producing
{
"items": {
"Soft Drinks": {
"T2": [
{
"name": "Bottled",
"T3": [
{
"name": "Lemon",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
},
{
"name": "Orange",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
},
{
"name": "Lemon",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
},
{
"name": "Orange",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
}
]
},
{
"name": "Fountain",
"T3": [
{
"name": "Lemon",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
},
{
"name": "Orange",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
},
{
"name": "Lemon",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
},
{
"name": "Orange",
"leaf": [
[
{
"name": "500 ML"
},
{
"name": "1 Litre"
}
]
]
}
]
}
]
}
}
}
Upvotes: 0
Views: 60
Reputation: 97312
You need to initialize you T3Array
inside the loop that starts with for (String t2Data : t1ChildList) { //...
. Otherwise, you will be adding the same objects to the same array twice.
Upvotes: 1