将Json对象转换为哈希图?

时间:2018-11-04 23:17:57

标签: java json hashmap gson

我有一个字符串对象,表示返回网络任务的json对象。我需要将其转换为Map(或HashMap)。我一直在使用gson,但未成功。这是json字符串(请原谅缩进,因为我不得不手动添加换行符):

{
"plans":{
"Ankle Recovery":{
"StartDate":"09/24/2018",
"Progress":0.6666666666666666,
"Tasks":[
        {
            "date":"10/16/2018",
            "amount":200,
            "task":"ice ankle for 30 min",
            "completed":true,
            "requirementType":"steps"},
            {
                "date":"10/17/2018",
                "amount":200,
                "task":"ice ankle for 30 min",
                "completed":true,
                "requirementType":"steps"
            },
            {
                "date":"10/18/2018",
                "amount":200,
                "task":"ice ankle for 30 min",
                "completed":false,
                "requirementType":"steps"
            }
],
"Username":"email@site.com",
"Doctor":"Mike Michaels",
"EndDate":"12/24/2018"}},
"status":true
}

这是我用来进行转换的代码:

private Map<String, String> plans;
plans = new Gson().fromJson(result, new TypeToken<Map<String, String>>() {}.getType());

既没有工作,也没有工作。我在Stack Overflow上尝试了一些不同的解决方案,但到目前为止都没有成功。

我也抛出了一个我不太了解的异常:

 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 11

(第11列在“ AnkleRecovery”中的第一条引号之前)

如果可能的话,我想使用简单的gson来完成这项工作。但我愿意接受其他解决方案。

2 个答案:

答案 0 :(得分:1)

您发布的JSON无效,第3行:

"Ankle Recovery" : {
//              / \
//  this is what you are missing

此工具将帮助您验证JSON结构并设置其格式:https://jsonlint.com/


现在是实际问题。您的JSON具有以下结构:

{
    "plans": Object,
    "status": Boolean,
}

这些都不是字符串(对象!=字符串,布尔值==字符串)。
此类结构无法映射到Map<String, String>,因为这要求该值必须是字符串。

您将需要创建多个POJO来定义您的结构,然后映射到这些POJO,例如:

class Project {
    public Map<String,Plan> plans;
    public Boolean status;
}

class Plan {
    public String StartDate;
    public Double Progress;
    public List<Task> tasks;
    ...
}

class Task {
    ...
}

答案 1 :(得分:0)

免责声明...

我总是会使用一个或多个POJO进行调查,如果可能的话,这些POJO可以用来表示数据结构。

没有更多信息,就无法知道诸如Ankle Recovery之类的键是否稳定,或者它们是否可能会更改。

“ A”可能的解决方案

通常,JSON采用键/值对的形式,其中值可能是另一个JSON对象,数组或其他值的列表,因此您可以“直接”处理结构,例如...

String text = "{\n"
        + "  \"plans\":{\n"
        + "    \"Ankle Recovery\":{\n"
        + "      \"StartDate\":\"09/24/2018\",\n"
        + "      \"Progress\":0.6666666666666666,\n"
        + "      \"Tasks\":[\n"
        + "        {\n"
        + "          \"date\":\"10/16/2018\",\n"
        + "          \"amount\":200,\n"
        + "          \"task\":\"ice ankle for 30 min\",\n"
        + "          \"completed\":true,\n"
        + "          \"requirementType\":\"steps\"\n"
        + "        },\n"
        + "        {\n"
        + "          \"date\":\"10/17/2018\",\n"
        + "          \"amount\":200,\n"
        + "          \"task\":\"ice ankle for 30 min\",\n"
        + "          \"completed\":true,\n"
        + "          \"requirementType\":\"steps\"\n"
        + "        },\n"
        + "        {\n"
        + "          \"date\":\"10/18/2018\",\n"
        + "          \"amount\":200,\n"
        + "          \"task\":\"ice ankle for 30 min\",\n"
        + "          \"completed\":false,\n"
        + "          \"requirementType\":\"steps\"\n"
        + "        }\n"
        + "      ],\n"
        + "      \"Username\":\"email@site.com\",\n"
        + "      \"Doctor\":\"Mike Michaels\",\n"
        + "      \"EndDate\":\"12/24/2018\"\n"
        + "    }\n"
        + "  },\n"
        + "  \"status\":true\n"
        + "}";

Gson gson = new Gson();
Map<String, Object> fromJson = gson.fromJson(text, Map.class);
Map<String, Object> plans = (Map<String, Object>) fromJson.get("plans");
Map<String, Object> recovery = (Map<String, Object>) plans.get("Ankle Recovery");
List<Map<String, Object>> tasks = (List<Map<String, Object>>) recovery.get("Tasks");
for (Map<String, Object> taks : tasks) {
    for (Map.Entry<String, Object> entry : taks.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    }
}

现在,这将为您提供...的输出

date = 10/16/2018
amount = 200.0
task = ice ankle for 30 min
completed = true
requirementType = steps
date = 10/17/2018
amount = 200.0
task = ice ankle for 30 min
completed = true
requirementType = steps
date = 10/18/2018
amount = 200.0
task = ice ankle for 30 min
completed = false
requirementType = steps

话虽如此,您自己的解析可能会涉及更多的工作,必须检查某些键是否存在并根据需要采取适当的操作