无法使用Gson将JSON反序列化为HashMAp

时间:2022-09-15 12:58:48

I am having trouble correctly deserialising JSON into POJO's that are in a Map

我无法正确地将JSON反序列化为Map中的POJO

I connect to a webservice that returns a JSON dictionary with an arbitary number of keys that looks like

我连接到一个Web服务,它返回一个JSON字典,其中包含任意数量的键

{ 
    "arbitray_key_name": {
        "foo_sticks": "objectName"
        "bar_socks": ["A", "B"]
    },
    "another_key_name: {
        "foo_sticks": "differentName"
        "bar_socks": ["C", "D", "E"]
    }
    ...
}

I am using Gson to deserialise into a HashMap. If I do

我正在使用Gson反序列化为HashMap。如果我做

HashMap<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>() {}.getType());

with the above JsonData (minus the ... obviously) then I get exactly what I expect which is a HashMap of 2 items, keys of 'arbitray_key_name', 'another_key_name', where each value is itself a Map with the correct key/value information for the child objects.

上述JsonData(减...显然)然后我得到正是我期待这是2项是一个HashMap,“arbitray_key_name”,“another_key_name”,其中每个值本身是用正确的键/值地图的钥匙子对象的信息。

However I have a POJO class that I want to deserialise the values into which looks like

但是我有一个POJO类,我想要反序列化其中的值

public class FooInfo {
    @Key("foo_sticks")
    public String fooSticks;

    @Key("bar_socks")
    public String[] barSocks;
}

If I try and do

如果我尝试做

HashMap<String, FooInfo> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, FooInfo>>() {}.getType());

then I get back a HashMap of two items where each value is a FooInfo but the fields in both the FooInfo objects are null, the Json hasn't been mapped into the objects. What am I doing wrong?

然后我找回两个项目的HashMap,其中每个值都是FooInfo,但两个FooInfo对象中的字段都是null,Json尚未映射到对象中。我究竟做错了什么?

1 个解决方案

#1


1  

I don't know where you got that @Key annotation from, but Gson uses @SerializedName.

我不知道你从哪里得到@Key注释,但Gson使用@SerializedName。

After making that change, I have the class:

做完这个改变之后,我有了这个课程:

public class FooInfo {
    @SerializedName("foo_sticks")
    public String fooSticks;

    @SerializedName("bar_socks")
    public String[] barSocks;

    @Override
    public String toString() {
        return "FooInfo [fooSticks=" + fooSticks + ", barSocks=" + Arrays.toString(barSocks) + "]";
    }
}

Then, parsing the JSON:

然后,解析JSON:

{ 
    "arbitray_key_name": {
        "foo_sticks": "objectName",
        "bar_socks": ["A", "B"]
    },
    "another_key_name": {
        "foo_sticks": "differentName",
        "bar_socks": ["C", "D", "E"]
    }
}

I can properly read in the object:

我可以在对象中正确读取:

HashMap<String, FooInfo> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, FooInfo>>() {}.getType());
System.out.println(map);

And get your expected output:

并获得您的预期输出:

{another_key_name=FooInfo [fooSticks=differentName, barSocks=[C, D, E]], arbitray_key_name=FooInfo [fooSticks=objectName, barSocks=[A, B]]}

#1


1  

I don't know where you got that @Key annotation from, but Gson uses @SerializedName.

我不知道你从哪里得到@Key注释,但Gson使用@SerializedName。

After making that change, I have the class:

做完这个改变之后,我有了这个课程:

public class FooInfo {
    @SerializedName("foo_sticks")
    public String fooSticks;

    @SerializedName("bar_socks")
    public String[] barSocks;

    @Override
    public String toString() {
        return "FooInfo [fooSticks=" + fooSticks + ", barSocks=" + Arrays.toString(barSocks) + "]";
    }
}

Then, parsing the JSON:

然后,解析JSON:

{ 
    "arbitray_key_name": {
        "foo_sticks": "objectName",
        "bar_socks": ["A", "B"]
    },
    "another_key_name": {
        "foo_sticks": "differentName",
        "bar_socks": ["C", "D", "E"]
    }
}

I can properly read in the object:

我可以在对象中正确读取:

HashMap<String, FooInfo> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, FooInfo>>() {}.getType());
System.out.println(map);

And get your expected output:

并获得您的预期输出:

{another_key_name=FooInfo [fooSticks=differentName, barSocks=[C, D, E]], arbitray_key_name=FooInfo [fooSticks=objectName, barSocks=[A, B]]}