So I'm working with this data in Java:
HashMap<String, String> currentValues = new HashMap<String, String>();
String currentID;
Timestamp currentTime;
String key;
Which I need to convert to this JSON:
{
"date" : "23098272362",
"id" : "123",
"key" : "secretkey",
"data" : [{
"type" : "x",
"value" : "y"
},{
"type" : "a",
"value" : "b"
}
]
}
But I can't quite work out how.
Currently I'm thinking this is the best approach:
JSONObject dataset = new JSONObject();
dataset.put("date", currentTime);
dataset.put("id", currentID);
dataset.put("key", key);
JSONArray payload = new JSONArray();
payload.add(dataset);
But I'm unsure how I'd do this with the Hashmap. I know it's something like this:
JSONObject data = new JSONObject();
Iterator it = currentValues.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
data.put("type", pair.getKey()) ;
data.put("value", pair.getValue()) ;
it.remove(); // avoids a ConcurrentModificationException
}
But the exact syntax and how I'd then add it alongside the other data I can't work out. Any ideas?