猿问

使用 elemental.json.* 类将 HashMap 转换为 JSON 字符串

使用旧的 org.json.* 类,我只需执行以下操作:


JSONObject jsonObject = new JSONObject(myHashMap);

String jsonString = jsonObject.toString();

我想知道如何使用 elemental.json.* 类来做到这一点。


慕码人2483693
浏览 175回答 2
2回答

MMTTMM

这是我当前用于转换为HashMap<String, Object>. 当为尚未满足的新类型抛出 RunTimeException 时,我将其添加进来。public static String hashMapToJsonString(HashMap hashMap){&nbsp; &nbsp; JsonObject json = hashMapToJsonObject(hashMap);&nbsp; &nbsp; return new json.toJson();}public static HashMap<String, Object> jsonStringToHashMap(String string) {&nbsp; &nbsp; JsonObject json = Json.parse(string.getRawString());&nbsp; &nbsp; HashMap<String, Object> retMap = new HashMap<String, Object>();&nbsp; &nbsp; if (!(json instanceof JreJsonNull)) {&nbsp; &nbsp; &nbsp; &nbsp; retMap = jsonObjectToHashMap(json);&nbsp; &nbsp; }&nbsp; &nbsp; return retMap;}private static JsonObject hashMapToJsonObject(HashMap hashMap) {&nbsp; &nbsp; JsonObject json = Json.createObject();&nbsp; &nbsp; Set<String> keySet = hashMap.keySet();&nbsp; &nbsp; for(String key : keySet){&nbsp; &nbsp; &nbsp; &nbsp; Object object = hashMap.get(key);&nbsp; &nbsp; &nbsp; &nbsp; if(object instanceof ArrayList){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.put(key, arrayListToJsonArray((ArrayList)object));&nbsp; &nbsp; &nbsp; &nbsp; } else if(object instanceof HashMap){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.put(key, hashMapToJsonObject((HashMap)object));&nbsp; &nbsp; &nbsp; &nbsp; } else if(object instanceof String) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.put(key, (String)object);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Type not catered for java->json conversion (in HashMap): " + object.getClass().getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return json;}private static JsonArray arrayListToJsonArray(ArrayList array) {&nbsp; &nbsp; JsonArray json = Json.createArray();&nbsp; &nbsp; int i = 0;&nbsp; &nbsp; for(Object object : array){&nbsp; &nbsp; &nbsp; &nbsp; if(object instanceof ArrayList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.set(i, arrayListToJsonArray((ArrayList)object));&nbsp; &nbsp; &nbsp; &nbsp; } else if(object instanceof HashMap) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.set(i, hashMapToJsonObject((HashMap)object));&nbsp; &nbsp; &nbsp; &nbsp; } else if(object instanceof String) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.set(i, (String)object);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Type not catered for java->json conversion (in array): " + object.getClass().getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; i ++;&nbsp; &nbsp; }&nbsp; &nbsp; return json;}private static HashMap<String, Object> jsonObjectToHashMap(JsonObject json) throws JsonException {&nbsp; &nbsp; HashMap<String, Object> map = new HashMap<String, Object>();&nbsp; &nbsp; String[] keys = json.keys();&nbsp; &nbsp; for(String key : keys){&nbsp; &nbsp; &nbsp; &nbsp; Object object = json.get(key);&nbsp; &nbsp; &nbsp; &nbsp; map.put(key, jsonObjectToJavaObject(object));&nbsp; &nbsp; }&nbsp; &nbsp; return map;}private static ArrayList jsonArrayToArrayList(JsonArray json) throws JsonException {&nbsp; &nbsp; ArrayList array = new ArrayList();&nbsp; &nbsp; for(int i = 0; i < json.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; Object object = json.get(i);&nbsp; &nbsp; &nbsp; &nbsp; array.add(jsonObjectToJavaObject(object));&nbsp; &nbsp; }&nbsp; &nbsp; return array;}private static Object jsonObjectToJavaObject(Object jsonObject) {&nbsp; &nbsp; if(jsonObject instanceof JsonArray) {&nbsp; &nbsp; &nbsp; &nbsp; return jsonArrayToArrayList((JsonArray) jsonObject);&nbsp; &nbsp; } else if(jsonObject instanceof JsonObject) {&nbsp; &nbsp; &nbsp; &nbsp; return jsonObjectToHashMap((JsonObject) jsonObject);&nbsp; &nbsp; } else if(jsonObject instanceof JreJsonString) {&nbsp; &nbsp; &nbsp; &nbsp; return ((JreJsonString)jsonObject).asString();&nbsp; &nbsp; } else if(jsonObject instanceof JreJsonNumber) {&nbsp; &nbsp; &nbsp; &nbsp; return new Double(((JreJsonNumber)jsonObject).asNumber());&nbsp; &nbsp; } else if(jsonObject instanceof JreJsonNull){&nbsp; &nbsp; &nbsp; &nbsp; return new Null();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Type not catered for json->java conversion: " + jsonObject.getClass().getName());&nbsp; &nbsp; }}

holdtom

假设你有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();
随时随地看视频慕课网APP

相关分类

Java
我要回答