【问题标题】:Gson fromJson deserializeGson fromJson 反序列化
【发布时间】:2020-02-05 19:42:51
【问题描述】:

我有以下 json:

[
    {
        "": "",
        "substituted_restday": "2020-02-01",
        "original_restday": "2020-02-08",
        "id": "15d13f70-c0a852c0-3a925f13-6dca1982",
        "_UNIQUEKEY_": "15d1592a-c0a852c0-3a925f13b7c65023",
        "parentId": ""
    }, 
    {
        "": "",
        "substituted_restday": "2020-02-03",
        "original_restday": "2020-02-09",
        "id": "15d14d55-c0a852c0-3a925f13-727b70af",
        "_UNIQUEKEY_": "15d1592a-c0a852c0-3a925f13-3711a584",
        "parentId": ""
    }
]

我想从 JSON 中获取“submitted_restday”和“original_restday”的值。所以我使用 gson.from 和下面的语法来将它整合到 JAVA 对象中。

String[] str = gson.fromJson(JSON, String[].class);

但是,它显示了以下错误:

com.google.gson.JsonParseException: The JsonDeserializer StringTypeAdapter failed to deserialize json object {"":"","substituted_restday":"2020-02-01","original_restday":"2020-02-08","id":"15d13f70-c0a852c0-3a925f13-6dca1982","_UNIQUEKEY_":"15d1592a-c0a852c0-3a925f13-b7c65023","parentId":""} given the type class java.lang.String

那么,我怎样才能从 JSON 中获取“submitted_restday”和“original_restday”的值呢?谢谢?

【问题讨论】:

    标签: java json gson


    【解决方案1】:

    树形结构

    Gson 可以将 JSON 解析为树结构,类似于 XML 可以解析为 DOM 树。树的节点可以是JsonElement 的以下子类之一:JsonObjectJsonArrayJsonPrimitiveJsonNull

    由于 JSON 以 [ 开头,因此您可以解析为 JsonArray,如下所示:

    JsonArray root = gson.fromJson(JSON, JsonArray.class);
    for (JsonElement elem : root) {
        JsonObject obj = elem.getAsJsonObject();
        String substituted_restday = obj.get("substituted_restday").getAsString();
        String original_restday = obj.get("original_restday").getAsString();
        System.out.printf("substituted_restday = '%s', original_restday = '%s'%n",
                          substituted_restday, original_restday);
    }
    

    地图列表

    Gson 可以将 JSON 对象解析成Map

    List<Map<String, String>> root = gson.fromJson(JSON, new TypeToken<List<Map<String, String>>>() {}.getType());
    for (Map<String, String> obj : root) {
        String substituted_restday = obj.get("substituted_restday");
        String original_restday = obj.get("original_restday");
        System.out.printf("substituted_restday = '%s', original_restday = '%s'%n",
                          substituted_restday, original_restday);
    }
    

    POJO 数组

    Gson 可以将 JSON 对象解析为 POJO。这是处理数据的最类型安全的方式。

    MyObj[] root = gson.fromJson(JSON, MyObj[].class);
    for (MyObj obj : root) {
        System.out.printf("substituted_restday = '%s', original_restday = '%s'%n",
                          obj.substitutedRestday, obj.originalRestday);
    }
    
    class MyObj {
        @SerializedName("")
        String blank;
        @SerializedName("substituted_restday")
        String substitutedRestday;
        @SerializedName("original_restday")
        String originalRestday;
        @SerializedName("id")
        String id;
        @SerializedName("_UNIQUEKEY_")
        String uniqueKey;
        @SerializedName("parentId")
        String parentId;
    }
    

    您可能希望在 POJO 上使用 getter 和 setter 方法。这只是一个简化的运行示例。


    输出(来自所有 3 个)

    substituted_restday = '2020-02-01', original_restday = '2020-02-08'
    substituted_restday = '2020-02-03', original_restday = '2020-02-09'
    

    【讨论】:

    • 我尝试使用 List of map 方法。但是它得到了“遇到:
    • @kaholee 没有看到你做了什么,我不知道。如果您需要帮助,请创建一个新问题。
    【解决方案2】:
    JsonObject jsonObject = gson.fromJson( JSON, JsonObject.class);
    

    然后

    jsonObject.get(substituted_restday); // returns a JsonElement for that name
    jsonObject.get(original_restday); 
    

    Imo,您应该为 json 元素创建一个 pojo,然后让 fromJson 方法返回该 pojo 的数组。此链接应该为您提供更多详细信息 - https://howtodoinjava.com/gson/gson-parse-json-array/

    【讨论】:

    • com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonArray
    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多