使用Jackson注释将子数组解析为JSON?

问题描述:

我正在尝试解析一些包含嵌套数组的JSON。我希望数组映射到父映射中的子对象列表。这是(稍微缩写)JSON和Java类

I'm trying to parse some JSON containing a nested array. I'd like the array to map to a list of child objects within the parent I'm mapping. Here is the (slightly abbreviated) JSON and Java classes

JSON:

{
    "id": "12121212121",
    "title": "Test Object",
    "media$content": [
        {
            "plfile$audioChannels": 1,
            "plfile$audioSampleRate": 18000,
        },
        {
            "plfile$audioChannels": 2,
            "plfile$audioSampleRate": 48000,
        },
        {
            "plfile$audioChannels": 2,
            "plfile$audioSampleRate": 48000,
        }
    ]
}

Java类

class MediaObject {
    @JsonProperty("id")
    private String id;

    @JsonProperty("title")
    private String title;

    @JsonProperty("media$Content")
    private List<MediaContent> mediaContent;

    ... getters/setters ...

}


class MediaContent {

    @JsonProperty("plfile$audioChannels")
    private int audioChannels;

    @JsonProperty("plfile$audioSampleRate")
    private int audioSampleRate;

    ... getters/setters ...
}

我希望能够使用注释和标准映射器代码反序列化,即
mapper.readValue(jsonString,MediaObject.class)

I'd like to be able to deserialize using annotations along with the standard mapper code, i.e. mapper.readValue(jsonString, MediaObject.class)

所有内容使用id和title字段工作正常,但我的MediaContent对象列表总是为空。这似乎杰克逊应该能够毫不费力地处理,有人能看到我在这里做错了什么吗?

Everything works fine with the "id" and "title" fields, but my list of MediaContent objects always comes up null. This seems like something Jackson should be able to handle without much trouble, can anyone see what I'm doing wrong here?

json字段的名称是错误的 - 属性不是 media $ Content ,而是 media $ [c] ontent 。否则我不明白为什么它不起作用。

The name of the json field is wrong - the attribute is not media$Content, rather media$[c]ontent. Otherwise I do not see why it will not work.