我使用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库。
当前回答
我只用了Gson
HashMap<String, Object> map = new Gson().fromJson(json.toString(), HashMap.class);
其他回答
您可以使用谷歌gson库转换json对象。
https://code.google.com/p/google-gson/
其他图书馆如Jackson也可以使用。
这不会将其转换为映射。但是你可以做任何你想做的事情。
想象一下你有如下的邮件列表。不受任何编程语言的限制,
emailsList = ["abc@gmail.com","def@gmail.com","ghi@gmail.com"]
下面是JAVA代码-用于将json转换为map
JSONObject jsonObj = new JSONObject().put("name","abc").put("email id",emailsList);
Map<String, Object> s = jsonObj.getMap();
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JsonUtils {
public static Map<String, Object> jsonToMap(JSONObject json) {
Map<String, Object> retMap = new HashMap<String, Object>();
if(json != null) {
retMap = toMap(json);
}
return retMap;
}
public static Map<String, Object> toMap(JSONObject object) {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keySet().iterator();
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) {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.size(); 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;
}
}
这是一个老问题,但可能仍然与某些人有关。 假设你有字符串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));
}
这就是我的方法,希望它能回答问题。
递归方式:
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);