我使用Java,我有一个JSON字符串:

{
"name" : "abc" ,
"email id " : ["abc@gmail.com","def@gmail.com","ghi@gmail.com"]
}

然后是我的Java地图:

Map<String, Object> retMap = new HashMap<String, Object>();

我想把所有来自JSONObject的数据存储在那个HashMap中。

有人能为此提供代码吗?我想用org。json库。


当前回答

使用Jackson转换:

JSONObject obj = new JSONObject().put("abc", "pqr").put("xyz", 5);

Map<String, Object> map = new ObjectMapper().readValue(obj.toString(), new TypeReference<Map<String, Object>>() {});

其他回答

你也可以使用Jackson API:

    final String json = "....your json...";
    final ObjectMapper mapper = new ObjectMapper();
    final MapType type = mapper.getTypeFactory().constructMapType(
        Map.class, String.class, Object.class);
    final Map<String, Object> data = mapper.readValue(json, type);

如果你想要无库版本,这里是与regex的解决方案:

public static HashMap<String, String> jsonStringToMap(String inputJsonString) {
    final String regex = "(?:\\\"|\\')(?<key>[\\w\\d]+)(?:\\\"|\\')(?:\\:\\s*)(?:\\\"|\\')?(?<value>[\\w\\s-]*)(?:\\\"|\\')?";
    HashMap<String, String> map = new HashMap<>();
    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(inputJsonString);

    while (matcher.find()) {
        for (int i = 1; i <= matcher.groupCount(); i++) {
            map.put(matcher.group("key"), matcher.group("value"));
        }
    }
    return map;
}

您可以使用谷歌gson库转换json对象。

https://code.google.com/p/google-gson/‎

其他图书馆如Jackson也可以使用。

这不会将其转换为映射。但是你可以做任何你想做的事情。

使用Gson,你可以做以下事情:

Map<String, Object> retMap = new Gson().fromJson(
    jsonString, new TypeToken<HashMap<String, Object>>() {}.getType()
);

递归方式:

public 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;
}

public 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;
}

使用Jackson库:

import com.fasterxml.jackson.databind.ObjectMapper;

Map<String, Object> mapping = new ObjectMapper().readValue(jsonStr, HashMap.class);