使用Jackson注释将子数组解析为JSON? [英] desrialize JSON with child array using Jackson annotations?

查看:223
本文介绍了使用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.

这篇关于使用Jackson注释将子数组解析为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆