从JSON字符串创建Hashmap

v1k*_*kas 42 java android json hashmap

从java中的json字符串创建一个hashmap?

我有像json字符串,{"phonetype":"N95","cat":"WP"}并希望转换为标准的Hashmap.

我该怎么做?

Vin*_*raj 61

解析JSONObject并创建HashMap

public static void jsonToMap(String t) throws JSONException {

        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject jObject = new JSONObject(t);
        Iterator<?> keys = jObject.keys();

        while( keys.hasNext() ){
            String key = (String)keys.next();
            String value = jObject.getString(key); 
            map.put(key, value);

        }

        System.out.println("json : "+jObject);
        System.out.println("map : "+map);
    }
Run Code Online (Sandbox Code Playgroud)

测试输出:

json : {"phonetype":"N95","cat":"WP"}
map : {cat=WP, phonetype=N95}
Run Code Online (Sandbox Code Playgroud)

  • 请指定您正在使用的库,json-simple中的JSONObject没有方法`keys()`或`getString()` (4认同)

Pur*_*ham 40

您可以使用Google的Gson库将json转换为Hashmap.试试下面的代码

String jsonString = "Your JSON string";
HashMap<String,String> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, String>>(){}.getType());
Run Code Online (Sandbox Code Playgroud)


Zie*_*bhi 13

public class JsonMapExample {

    public static void main(String[] args) {
        String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}";
        Map<String, String> map = new HashMap<String, String>();
        ObjectMapper mapper = new ObjectMapper();

        try {
            //convert JSON string to Map
            map = mapper.readValue(json, new TypeReference<HashMap<String, String>>() {});
            System.out.println(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

{phonetype=N95, cat=WP}
Run Code Online (Sandbox Code Playgroud)

你可以看到这个链接有用http://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/


Bla*_*laz 7

你可以使用Gson

Type type = new TypeToken<HashMap<String, String>>() {}.getType();
new Gson().fromJson(jsonString, type);
Run Code Online (Sandbox Code Playgroud)


Nat*_*hat 5

这是简单的操作,不需要使用任何外部库。

您可以改用此类:)(甚至可以处理列表、嵌套列表和 json)

public class Utility {

    public static Map<String, Object> jsonToMap(Object json) throws JSONException {

        if(json instanceof JSONObject)
            return _jsonToMap_((JSONObject)json) ;

        else if (json instanceof String)
        {
            JSONObject jsonObject = new JSONObject((String)json) ;
            return _jsonToMap_(jsonObject) ;
        }
        return null ;
    }


   private static Map<String, Object> _jsonToMap_(JSONObject json) throws JSONException {
        Map<String, Object> retMap = new HashMap<String, Object>();

        if(json != JSONObject.NULL) {
            retMap = toMap(json);
        }
        return retMap;
    }


    private static Map<String, Object> toMap(JSONObject object) throws JSONException {
        Map<String, Object> map = new HashMap<String, Object>();

        Iterator<String> keysItr = object.keys();
        while(keysItr.hasNext()) {
            String key = keysItr.next();
            Object value = object.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

要将JSON 字符串转换为 hashmap,请使用以下命令:

HashMap<String, Object> hashMap = new HashMap<>(Utility.jsonToMap(response)) ;
Run Code Online (Sandbox Code Playgroud)