Convert JSONObject to Map

后端 未结 6 1895
抹茶落季
抹茶落季 2020-12-08 18:20

I have a JSONObject with some attributes that I want to convert into a Map

Is there something that I can use from the

相关标签:
6条回答
  • 2020-12-08 18:48

    use Jackson(http://jackson.codehaus.org/) from http://json.org/

    HashMap<String,Object> result =
            new ObjectMapper().readValue(<JSON_OBJECT>, HashMap.class);
    
    0 讨论(0)
  • 2020-12-08 18:49

    The best way to convert it to HashMap<String, Object> is this:

    HashMap<String, Object> result = new ObjectMapper().readValue(jsonString, new TypeReference<Map<String, Object>>(){}));
    
    0 讨论(0)
  • 2020-12-08 18:50

    Note to the above solution (from A Paul): The solution doesn't work, cause it doesn't reconstructs back a HashMap< String, Object > - instead it creates a HashMap< String, LinkedHashMap >.

    Reason why is because during demarshalling, each Object (JSON marshalled as a LinkedHashMap) is used as-is, it takes 1-on-1 the LinkedHashMap (instead of converting the LinkedHashMap back to its proper Object).

    If you had a HashMap< String, MyOwnObject > then proper demarshalling was possible - see following example:

    ObjectMapper mapper = new ObjectMapper();
    TypeFactory typeFactory = mapper.getTypeFactory();
    MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, MyOwnObject.class);
    HashMap<String, MyOwnObject> map = mapper.readValue(new StringReader(hashTable.toString()), mapType);
    
    0 讨论(0)
  • 2020-12-08 18:53

    You can use Gson() (com.google.gson) library if you find any difficulty using Jackson.

    HashMap<String, Object> yourHashMap = new Gson().fromJson(yourJsonObject.toString(), HashMap.class);
    
    0 讨论(0)
  • 2020-12-08 18:56

    Found out these problems can be addressed by using

    ObjectMapper#convertValue(Object fromValue, Class<T> toValueType)
    

    As a result, the origal quuestion can be solved in a 2-step converison:

    1. Demarshall the JSON back to an object - in which the Map<String, Object> is demarshalled as a HashMap<String, LinkedHashMap>, by using bjectMapper#readValue().

    2. Convert inner LinkedHashMaps back to proper objects

    ObjectMapper mapper = new ObjectMapper(); Class clazz = (Class) Class.forName(classType); MyOwnObject value = mapper.convertValue(value, clazz);

    To prevent the 'classType' has to be known in advance, I enforced during marshalling an extra Map was added, containing <key, classNameString> pairs. So at unmarshalling time, the classType can be extracted dynamically.

    0 讨论(0)
  • 2020-12-08 19:10

    This is what worked for me:

        public static Map<String, Object> toMap(JSONObject jsonobj)  throws JSONException {
            Map<String, Object> map = new HashMap<String, Object>();
            Iterator<String> keys = jsonobj.keys();
            while(keys.hasNext()) {
                String key = keys.next();
                Object value = jsonobj.get(key);
                if (value instanceof JSONArray) {
                    value = toList((JSONArray) value);
                } else if (value instanceof JSONObject) {
                    value = toMap((JSONObject) value);
                }   
                map.put(key, value);
            }   return map;
        }
    
        public static List<Object> toList(JSONArray array) throws JSONException {
            List<Object> list = new ArrayList<Object>();
            for(int i = 0; i < array.length(); i++) {
                Object value = array.get(i);
                if (value instanceof JSONArray) {
                    value = toList((JSONArray) value);
                }
                else if (value instanceof JSONObject) {
                    value = toMap((JSONObject) value);
                }
                list.add(value);
            }   return list;
    }
    

    Most of this is from this question: How to convert JSONObject to new Map for all its keys using iterator java

    0 讨论(0)
提交回复
热议问题