所以我正在用 Java 处理这些数据:
HashMap<String, String> currentValues = new HashMap<String, String>();
String currentID;
Timestamp currentTime;
String key;
我需要将其转换为此 JSON:
{
"date" : "23098272362",
"id" : "123",
"key" : "secretkey",
"data" : [{
"type" : "x",
"value" : "y"
},{
"type" : "a",
"value" : "b"
}
]
}
但我不知道怎么做。
目前我认为这是最好的方法:
JSONObject dataset = new JSONObject();
dataset.put("date", currentTime);
dataset.put("id", currentID);
dataset.put("key", key);
JSONArray payload = new JSONArray();
payload.add(dataset);
但我不确定如何使用 Hashmap 做到这一点。我知道它是这样的:
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
}
但是确切的语法以及我如何将它与我无法解决的其他数据一起添加。有任何想法吗?