My problem is that I need to create a JSon file looking like this in java :
"filename":"51958785100014_220930140958.zip",
"lotUUID":"51958785100014_220930140958",
"version":"0.1.0",
"lotCreationDate":"2022-09-30T14:21:14",
"zipCreationDate":"2022-09-30T14:21:14",
"contentSize":"3",
"content":
[
{
"orderUUID":"51958785100014_220930140958_991234",
"orderMetadataPath":"./51958785100014_220930140958_991234",
"orderMetadata":"51958785100014_220930140958_991234_order.json"
},
{
"orderUUID":"51958785100014_220930140958_991235",
"orderMetadataPath":"./51958785100014_220930140958_991235",
"orderMetadata":"51958785100014_220930140958_991235_order.json"
},
{
"orderUUID":"51958785100014_220930140958_991236",
"orderMetadataPath":"./51958785100014_220930140958_991236",
"orderMetadata":"51958785100014_220930140958_991236_order.json"
}
]
}
with the number of part in "content" equal to "contentsize".
For the moment, I wrote this :
import org.json.JSONObject;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
public class jsonLotUUID
{
public String orderUUID;
public jsonLotUUID(String fileNameText, String lotUUID, String date, Integer nbOrder)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put("filename", lotUUID+".zip");
jsonObject.put("lotUUID", lotUUID);
jsonObject.put("version", "0.1.0");
jsonObject.put("lotCreationDate", date);
jsonObject.put("zipCreationDate", date);
jsonObject.put("contentSize", nbOrder);
HashMap<String, String> m1 = new HashMap<String, String>();
for (int count = 1; count <= nbOrder; count++)
{
orderUUID = lotUUID+"-"+count;
m1.put("orderUUID", orderUUID);
m1.put("orderMetadataPath","./"+orderUUID);
m1.put("orderMetadata", orderUUID+"_order.json");
jsonObject.accumulate("content", m1);
}
try {
FileWriter file = new FileWriter("TestFiles/"+fileNameText+"/"+lotUUID+".json");
file.write(jsonObject.toString());
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
But I don’t get the result I want, I have the right number of iteration in the "content" part, but it’s the same one everytime.
Can someone give me some idea/advice to help ?
>Solution :
You need to put
HashMap<String, String> m1 = new HashMap<String, String>();
inside for loop.