我使用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库。
我使用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库。
当前回答
递归方式:
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);
其他回答
您可以使用谷歌gson库转换json对象。
https://code.google.com/p/google-gson/
其他图书馆如Jackson也可以使用。
这不会将其转换为映射。但是你可以做任何你想做的事情。
这是一个老问题,但可能仍然与某些人有关。 假设你有字符串HashMap hash和JsonObject JsonObject。
1)定义键列表。 例子:
ArrayList<String> keyArrayList = new ArrayList<>();
keyArrayList.add("key0");
keyArrayList.add("key1");
2)创建foreach循环,从jsonObject中添加哈希:
for(String key : keyArrayList){
hash.put(key, jsonObject.getString(key));
}
这就是我的方法,希望它能回答问题。
希望这能起作用,试试这个:
import com.fasterxml.jackson.databind.ObjectMapper;
Map<String, Object> response = new ObjectMapper().readValue(str, HashMap.class);
str,你的JSON字符串
就这么简单,如果你想要电子邮件,
String emailIds = response.get("email id").toString();
如果你想要无库版本,这里是与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;
}
使用JSON -simple,您可以将数据JSON转换为映射和映射到JSON。
try
{
JSONObject obj11 = new JSONObject();
obj11.put(1, "Kishan");
obj11.put(2, "Radhesh");
obj11.put(3, "Sonal");
obj11.put(4, "Madhu");
Map map = new HashMap();
obj11.toJSONString();
map = obj11;
System.out.println(map.get(1));
JSONObject obj12 = new JSONObject();
obj12 = (JSONObject) map;
System.out.println(obj12.get(1));
}
catch(Exception e)
{
System.err.println("EROR : 01 :"+e);
}