Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Gson将JSON转换为HashMap?_Java_Json_Dictionary_Hashmap_Gson - Fatal编程技术网

Java 如何使用Gson将JSON转换为HashMap?

Java 如何使用Gson将JSON转换为HashMap?,java,json,dictionary,hashmap,gson,Java,Json,Dictionary,Hashmap,Gson,我从一个服务器请求数据,该服务器返回JSON格式的数据。在发出请求时将HashMap转换为JSON一点也不难,但另一种方式似乎有点棘手。JSON响应如下所示: { “标题”:{ “警报”:[ { “警报ID”:“2”, “TSExpires”:空, “目标”:“1”, “文本”:“woot”, “类型”:“1” }, { “警报ID”:“3”, “TSExpires”:空, “目标”:“1”, “文本”:“woot”, “类型”:“1” } ], “会话”:“0bc8d0835f93ac3eb

我从一个服务器请求数据,该服务器返回JSON格式的数据。在发出请求时将HashMap转换为JSON一点也不难,但另一种方式似乎有点棘手。JSON响应如下所示:

{
“标题”:{
“警报”:[
{
“警报ID”:“2”,
“TSExpires”:空,
“目标”:“1”,
“文本”:“woot”,
“类型”:“1”
},
{ 
“警报ID”:“3”,
“TSExpires”:空,
“目标”:“1”,
“文本”:“woot”,
“类型”:“1”
}
],
“会话”:“0bc8d0835f93ac3ebbf11560b2c5be9a”
},
“结果”:“4be26bc400d3c”
}

访问此数据的最简单方法是什么?我正在使用GSON模块。

JSONObject通常在内部使用
HashMap
来存储数据。因此,您可以在代码中将其用作映射

例如

JSONObject obj = JSONObject.fromObject(strRepresentation);
Iterator i = obj.entrySet().iterator();
while (i.hasNext()) {
   Map.Entry e = (Map.Entry)i.next();
   System.out.println("Key: " + e.getKey());
   System.out.println("Value: " + e.getValue());
}

我知道这是一个相当老的问题,但我正在寻找一个解决方案,将嵌套的JSON反序列化为
映射
,但没有找到任何结果

按照我的yaml反序列化程序的工作方式,当您不指定类型时,它会将JSON对象默认为
Map
,但gson似乎不会这样做。幸运的是,您可以使用自定义反序列化程序来完成它

我使用下面的反序列化程序自然地反序列化任何内容,将
JsonObject
s默认为
Map
并将
JsonArray
s默认为
Object[]
s,其中所有子对象都类似地反序列化

private static class NaturalDeserializer implements JsonDeserializer<Object> {
  public Object deserialize(JsonElement json, Type typeOfT, 
      JsonDeserializationContext context) {
    if(json.isJsonNull()) return null;
    else if(json.isJsonPrimitive()) return handlePrimitive(json.getAsJsonPrimitive());
    else if(json.isJsonArray()) return handleArray(json.getAsJsonArray(), context);
    else return handleObject(json.getAsJsonObject(), context);
  }
  private Object handlePrimitive(JsonPrimitive json) {
    if(json.isBoolean())
      return json.getAsBoolean();
    else if(json.isString())
      return json.getAsString();
    else {
      BigDecimal bigDec = json.getAsBigDecimal();
      // Find out if it is an int type
      try {
        bigDec.toBigIntegerExact();
        try { return bigDec.intValueExact(); }
        catch(ArithmeticException e) {}
        return bigDec.longValue();
      } catch(ArithmeticException e) {}
      // Just return it as a double
      return bigDec.doubleValue();
    }
  }
  private Object handleArray(JsonArray json, JsonDeserializationContext context) {
    Object[] array = new Object[json.size()];
    for(int i = 0; i < array.length; i++)
      array[i] = context.deserialize(json.get(i), Object.class);
    return array;
  }
  private Object handleObject(JsonObject json, JsonDeserializationContext context) {
    Map<String, Object> map = new HashMap<String, Object>();
    for(Map.Entry<String, JsonElement> entry : json.entrySet())
      map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class));
    return map;
  }
}
然后像这样称呼它:

Object natural = gson.fromJson(source, Object.class);

我不确定为什么这不是gson中的默认行为,因为它存在于大多数其他半结构化序列化库中…

以下是我一直在使用的:

public static HashMap<String, Object> parse(String json) {
    JsonObject object = (JsonObject) parser.parse(json);
    Set<Map.Entry<String, JsonElement>> set = object.entrySet();
    Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();
    HashMap<String, Object> map = new HashMap<String, Object>();
    while (iterator.hasNext()) {
        Map.Entry<String, JsonElement> entry = iterator.next();
        String key = entry.getKey();
        JsonElement value = entry.getValue();
        if (!value.isJsonPrimitive()) {
            map.put(key, parse(value.toString()));
        } else {
            map.put(key, value.getAsString());
        }
    }
    return map;
}
publicstatichashmap解析(字符串json){
JsonObject object=(JsonObject)parser.parse(json);
Set=object.entrySet();
迭代器迭代器=set.Iterator();
HashMap=newHashMap();
while(iterator.hasNext()){
Map.Entry=iterator.next();
String key=entry.getKey();
JsonElement value=entry.getValue();
如果(!value.isJsonPrimitive()){
map.put(key,parse(value.toString());
}否则{
put(key,value.getAsString());
}
}
返回图;
}
此代码适用于:

Gson gson = new Gson(); 
String json = "{\"k1\":\"v1\",\"k2\":\"v2\"}";
Map<String,Object> map = new HashMap<String,Object>();
map = (Map<String,Object>) gson.fromJson(json, map.getClass());
Gson-Gson=new-Gson();
字符串json=“{\'k1\':\'v1\',\'k2\':\'v2\'”;
Map Map=newhashmap();
map=(map)gson.fromJson(json,map.getClass());

试试这个,它会有用的。我将其用于哈希表

public static Hashtable<Integer, KioskStatusResource> parseModifued(String json) {
    JsonObject object = (JsonObject) new com.google.gson.JsonParser().parse(json);
    Set<Map.Entry<String, JsonElement>> set = object.entrySet();
    Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();

    Hashtable<Integer, KioskStatusResource> map = new Hashtable<Integer, KioskStatusResource>();

    while (iterator.hasNext()) {
        Map.Entry<String, JsonElement> entry = iterator.next();

        Integer key = Integer.parseInt(entry.getKey());
        KioskStatusResource value = new Gson().fromJson(entry.getValue(), KioskStatusResource.class);

        if (value != null) {
            map.put(key, value);
        }

    }
    return map;
}
公共静态哈希表parseModified(字符串json){ JsonObject object=(JsonObject)new com.google.gson.JsonParser().parse(json); Set=object.entrySet(); 迭代器迭代器=set.Iterator(); Hashtable map=新的Hashtable(); while(iterator.hasNext()){ Map.Entry=iterator.next(); Integer key=Integer.parseInt(entry.getKey()); KioskStatusResource value=new Gson().fromJson(entry.getValue(),KioskStatusResource.class); if(值!=null){ map.put(键、值); } } 返回图; }
KioskStatusResource替换为类,将Integer替换为键类。

我已经克服了自定义JsonDeSerializer的类似问题。我试图让它有点通用,但仍然不够。这是一个适合我需要的解决方案

首先,您需要为映射对象实现一个新的JsonDeserializer

public class MapDeserializer<T, U> implements JsonDeserializer<Map<T, U>>
公共类MapDeserializer实现JsonDeserializer
反序列化方法将类似于此:

public Map<T, U> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

        if (!json.isJsonObject()) {
            return null;
        }

        JsonObject jsonObject = json.getAsJsonObject();
        Set<Entry<String, JsonElement>> jsonEntrySet = jsonObject.entrySet();
        Map<T, U> deserializedMap = new HashMap<T, U>();

        for (Entry<java.lang.String, JsonElement> entry : jsonEntrySet) {
            try {
                U value = context.deserialize(entry.getValue(), getMyType());
                deserializedMap.put((T) entry.getKey(), value);
            } catch (Exception ex) {
                logger.info("Could not deserialize map.", ex);
            }
        }

        return deserializedMap;
    }
公共映射反序列化(JsonElement json,类型typeOfT,JsonDeserializationContext) 抛出JsonParseException{ 如果(!json.isJsonObject()){ 返回null; } JsonObject JsonObject=json.getAsJsonObject(); Set jsonEntrySet=jsonObject.entrySet(); Map deserializedMap=新HashMap(); for(条目:jsonEntrySet){ 试一试{ U value=context.deserialize(entry.getValue(),getMyType()); 反序列化map.put((T)entry.getKey(),value); }捕获(例外情况除外){ logger.info(“无法反序列化映射。”,例如); } } 返回反序列化的dmap; } 此解决方案的缺点是,我的地图的键始终是“String”类型。然而,通过改变一些东西,有人可以使它通用。此外,我需要说,值的类应该在构造函数中传递。因此,我的代码中的方法
getMyType()
返回映射值的类型,该值在构造函数中传递

您可以参考这篇文章,以了解有关自定义反序列化程序的更多信息。

给您:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson("{'k1':'apple','k2':'orange'}", type);
import java.lang.reflect.Type;
导入com.google.gson.reflect.TypeToken;
Type Type=new-TypeToken(){}.getType();
Map myMap=gson.fromJson(“{'k1':'apple','k2':'orange'}”,类型);

新Gson库的更新:
现在您可以直接解析嵌套的Json来映射,但您应该注意,如果您试图将Json解析为
Map
type,它将引发异常。要解决这个问题,只需将结果声明为
LinkedTreeMap
type。示例如下:

String nestedJSON = "{\"id\":\"1\",\"message\":\"web_didload\",\"content\":{\"success\":1}}";
Gson gson = new Gson();
LinkedTreeMap result = gson.fromJson(nestedJSON , LinkedTreeMap.class);

我问了完全相同的问题,最后来到这里。我有一个看起来简单得多的不同方法(可能是新版本的gson?)

Gson-Gson=new-Gson();
Map jsonObject=(Map)gson.fromJson(数据,Object.class);
使用以下json

{
“map-00”:{
“数组-00”:[
“entry-00”,
“条目-01”
],
“值”:“条目-02”
}
}
以下

mapMap00=(Map)jsonObject.get(“Map-00”);
List array00=(List)map00.get(“array-00”);
字符串值=(字符串)map00.get(“值”);
for(int)
String nestedJSON = "{\"id\":\"1\",\"message\":\"web_didload\",\"content\":{\"success\":1}}";
Gson gson = new Gson();
LinkedTreeMap result = gson.fromJson(nestedJSON , LinkedTreeMap.class);
map-00.array-00[0]= entry-00
map-00.array-00[1]= entry-01
map-00.value = entry-02
Gson gson = new Gson();
HashMap<String, Object> fields = gson.fromJson(json, HashMap.class);
Map map = gson.fromJson(jsonString, Map.class);
String jsonString = "{'header': {'alerts': [{'AlertID': '2', 'TSExpires': null, 'Target': '1', 'Text': 'woot', 'Type': '1'}, {'AlertID': '3', 'TSExpires': null, 'Target': '1', 'Text': 'woot', 'Type': '1'}], 'session': '0bc8d0835f93ac3ebbf11560b2c5be9a'}, 'result': '4be26bc400d3c'}";
Map map = gson.fromJson(jsonString, Map.class);
System.out.println(map.getClass().toString());
System.out.println(map);
class com.google.gson.internal.LinkedTreeMap
{header={alerts=[{AlertID=2, TSExpires=null, Target=1, Text=woot, Type=1}, {AlertID=3, TSExpires=null, Target=1, Text=woot, Type=1}], session=0bc8d0835f93ac3ebbf11560b2c5be9a}, result=4be26bc400d3c}
 HashMap<String, String> jsonToMap(String JsonDetectionString) throws JSONException {

    HashMap<String, String> map = new HashMap<String, String>();
    Gson gson = new Gson();

    map = (HashMap<String, String>) gson.fromJson(JsonDetectionString, map.getClass());

    return map;

}
public static Type getMapType(Class keyType, Class valueType){
    return TypeToken.getParameterized(HashMap.class, keyType, valueType).getType();
}

public static  <K,V> HashMap<K,V> fromMap(String json, Class<K> keyType, Class<V> valueType){
    return gson.fromJson(json, getMapType(keyType,valueType));
}
HashMap<String, Object> myMap =
   gson.fromJson(yourJson, new TypeToken<HashMap<String, Object>>(){}.getType());
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;
    }
}
HashMap<String, Object> hashMap = new HashMap<>(Utility.jsonToMap(response)) ;
private Object handlePrimitive(JsonPrimitive json) {
  if(json.isBoolean()) {
    return json.getAsBoolean();
  } else if(json.isString())
    return json.getAsString();
  }

  Number num = element.getAsNumber();

  if(num instanceof Integer){
    map.put(fieldName, num.intValue());
  } else if(num instanceof Long){
    map.put(fieldName, num.longValue());
  } else if(num instanceof Float){
    map.put(fieldName, num.floatValue());
  } else {    // Double
     map.put(fieldName, num.doubleValue());
  }
}