I want to create a json with the below format,
How can i add many objects into a single array.
{
Data: [
{"dataType":"com.google.weight",
"startDate":"2021-04-1308:00",
"endDate":"2021-04-13",
"Weight":"65"},
{"dataType":"com.google.weight",
"startDate":"2021-04-1308:00",
"endDate":"2021-04-13",
"Weight":"85"},
{"dataType":"com.google.weight",
"startDate":"2021-04-1308:00",
"endDate":"2021-04-13",
"Weight":"95"}
]
}
Code:
JSONObject jsonObject = new JSONObject();
JSONObject json = new JSONObject();
jsonObject.put("dataType", dataSet.getDataType().getName().toString());
jsonObject.put("startDate", dateFormat.format(dp.getStartTime().toString());
jsonObject.put("endDate", dateFormat.format(dp.getEndTime().toString());
jsonObject.put("Weight", dp.getValue(field).toString());
json.put("Data", jsonObject) //this is wrong i guess. it replaces all the old values
Using the above code i get the below result. This is replacing all the other values and printing only one object. I want an array of objects. any help would be great!!!
{
Data:
{"dataType":"com.google.weight",
"startDate":"2021-04-1308:00",
"endDate":"2021-04-13",
"Weight":"95"}
}