使用旧的org.json。*类,我将简单地执行以下操作:
JSONObject jsonObject = new JSONObject(myHashMap);
String jsonString = jsonObject.toString();
我想知道如何使用elemental.json。*类做到这一点。
答案 0 :(得分:0)
假设您有Map<String, String>
,则可以执行以下操作:
Map<String, String> map = ...
JsonObject mapObj = Json.createObject();
map.entrySet().forEach(e -> mapObj.put(e.getKey(), e.getValue()));
String json = mapObj.toJson();
答案 1 :(得分:0)
这是我目前用于与HashMap<String, Object>
之间进行转换的解决方案。当为尚未满足的新类型引发RunTimeException时,我将其添加。
public static String hashMapToJsonString(HashMap hashMap){
JsonObject json = hashMapToJsonObject(hashMap);
return new json.toJson();
}
public static HashMap<String, Object> jsonStringToHashMap(String string) {
JsonObject json = Json.parse(string.getRawString());
HashMap<String, Object> retMap = new HashMap<String, Object>();
if (!(json instanceof JreJsonNull)) {
retMap = jsonObjectToHashMap(json);
}
return retMap;
}
private static JsonObject hashMapToJsonObject(HashMap hashMap) {
JsonObject json = Json.createObject();
Set<String> keySet = hashMap.keySet();
for(String key : keySet){
Object object = hashMap.get(key);
if(object instanceof ArrayList){
json.put(key, arrayListToJsonArray((ArrayList)object));
} else if(object instanceof HashMap){
json.put(key, hashMapToJsonObject((HashMap)object));
} else if(object instanceof String) {
json.put(key, (String)object);
} else {
throw new RuntimeException("Type not catered for java->json conversion (in HashMap): " + object.getClass().getName());
}
}
return json;
}
private static JsonArray arrayListToJsonArray(ArrayList array) {
JsonArray json = Json.createArray();
int i = 0;
for(Object object : array){
if(object instanceof ArrayList) {
json.set(i, arrayListToJsonArray((ArrayList)object));
} else if(object instanceof HashMap) {
json.set(i, hashMapToJsonObject((HashMap)object));
} else if(object instanceof String) {
json.set(i, (String)object);
} else {
throw new RuntimeException("Type not catered for java->json conversion (in array): " + object.getClass().getName());
}
i ++;
}
return json;
}
private static HashMap<String, Object> jsonObjectToHashMap(JsonObject json) throws JsonException {
HashMap<String, Object> map = new HashMap<String, Object>();
String[] keys = json.keys();
for(String key : keys){
Object object = json.get(key);
map.put(key, jsonObjectToJavaObject(object));
}
return map;
}
private static ArrayList jsonArrayToArrayList(JsonArray json) throws JsonException {
ArrayList array = new ArrayList();
for(int i = 0; i < json.length(); i++) {
Object object = json.get(i);
array.add(jsonObjectToJavaObject(object));
}
return array;
}
private static Object jsonObjectToJavaObject(Object jsonObject) {
if(jsonObject instanceof JsonArray) {
return jsonArrayToArrayList((JsonArray) jsonObject);
} else if(jsonObject instanceof JsonObject) {
return jsonObjectToHashMap((JsonObject) jsonObject);
} else if(jsonObject instanceof JreJsonString) {
return ((JreJsonString)jsonObject).asString();
} else if(jsonObject instanceof JreJsonNumber) {
return new Double(((JreJsonNumber)jsonObject).asNumber());
} else if(jsonObject instanceof JreJsonNull){
return new Null();
} else {
throw new RuntimeException("Type not catered for json->java conversion: " + jsonObject.getClass().getName());
}
}