将JSON文档反序列化为Java对象(arraylist错误)

将JSON文档反序列化为Java对象(arraylist错误),java,json,git,arraylist,Java,Json,Git,Arraylist,我已经设法从我正在使用的json文件中获取了许多字段id、标题、日期等,但当涉及到参与者字段时,我似乎无法使其正常工作。如何让参与者arraylist与其他字段一起工作?这是我的密码: Application.java 控制台输出 示例json文件 如果我正确理解了您的问题,您只需要定义对象类中的参与者列表。我看不到你的代码是如何定义参与者的,也看不到你所犯的错误 一个简单的例子,拥有类对象: 对于JSON文件: { "id": "68245603", "title": "Not

我已经设法从我正在使用的json文件中获取了许多字段id、标题、日期等,但当涉及到参与者字段时,我似乎无法使其正常工作。如何让参与者arraylist与其他字段一起工作?这是我的密码:

Application.java

控制台输出

示例json文件


如果我正确理解了您的问题,您只需要定义对象类中的参与者列表。我看不到你的代码是如何定义参与者的,也看不到你所犯的错误

一个简单的例子,拥有类对象:

对于JSON文件:

{
    "id": "68245603",
    "title": "Note Card, New Institue Flyer, 2013",   
    "date": "2013",   
    "participants": [
        {
            "person_name": "Karel Martens"
        },
        {
            "person_name": "Karel Martens"
        }
    ]
}
守则:

Object objects = new ObjectMapper().readValue(jsonFIle, Object.class);

也将使用参与者填充对象。

对象需要参与者的引用。比如:私人ArrayList参与者;还有能手/二传手。@honk-谢谢你的回答。我应该向你们展示我的参与者和角色类以及对象类,但无论如何我都非常类似于你们,所以这很好。目前,上面提到的对象id/标题等出现在控制台中,但ArrayList没有。我猜这并不像将\nParticpant:+participant添加到toString for角色一样简单\编辑:在控制台中,参与者/角色返回Participant:Null和Role:nullEhm,我只是编辑了答案以稍微改进其形状。答案本身由@magnificus提供。@honk哦,很抱歉搞混了,我会问他们的,谢谢@放大镜-嘿,你能看一下我留给honk的上面的评论吗?我不确定你们两个中的哪一个回答了,谁编辑了。@JoeFinield91在我看到你的完整代码之前,我无法理解什么是错的。在我的示例中,如果你为对象和参与者添加toString方法,你将得到以下输出:ID:68245603 Title:Note Card,新学院传单,2013年日期:2013年参与者[Karel Martens,Karel Martens]
Processing Object file: /Users/Joewinfield/Documents/JavaWorkspace/git/collection/objects/682/456...
ID: 68245603
Title: Note Card, New Institue Flyer, 2013
Date: 2013
Medium: Offset lithograph on paper
Credit: Gift of Karel Martens
Description: Black flyer with white text and three holes.
Gallery Text: null
--------------------------------------------------------------------------------------------------------------------------------------
Processing Object file: /Users/Joewinfield/Documents/JavaWorkspace/git/collection/objects/682/456...
ID: 68245607
Title: Mailer, New Institute Announcement, 2013
Date: 2013
Medium: Offset lithograph on paper
Credit: Gift of Karel Martens
Description: Black folded announcement printed with white text.
Gallery Text: null
{
    "id": "68245603",
    "tms:id": "276634",
    "accession_number": "2014-2-4",
    "title": "Note Card, New Institue Flyer, 2013",
    "title_raw": "New Institue Flyer",
    "url": "http:\/\/collection.cooperhewitt.org\/objects\/68245603\/",
    "department_id": "35347493",
    "period_id": null,
    "media_id": "35399021",
    "type_id": "68245601",
    "date": "2013",
    "year_start": 2013,
    "year_end": 2013,
    "year_acquired": "2014",
    "decade": "2010",
    "woe:country_id": "23424909",
    "medium": "Offset lithograph on paper",
    "markings": null,
    "signed": null,
    "inscribed": null,
    "provenance": "Karel Martens; 2014: acquired by Museum",
    "dimensions": "21 x 10.5 cm (8 1\/4 x 4 1\/8 in.)",
    "dimensions_raw": {
        "height": [
            "21.00",
            "centimeters"
        ],
        "width": [
            "10.50",
            "centimeters"
        ]
    },
    "creditline": "Gift of Karel Martens",
    "description": "Black flyer with white text and three holes.",
    "justification": "When Martens is asked how much do you need to represent something, his answer is \"no more than necessary.\" What drives his work is sparsity, limitation, constraint. This attitude is perfectly expressed in the interim identity he designed for the New Institute (launched 2013), which combines three existing institutions: The Netherlands Architecture Institute (NAI); Premsela, The Netherlands Institute for Design and Fashion; and Virtueel Platform, for e-culture. With no money available for the intervention, Martens took the graphic identity designed by Bruce Mau for the Netherlands Architecture Institute (represented in the CHNDM collection, 2000-43-1\/13) and printed over it in black, allowing enough of the old identity to show through to reveal the history behind the project, and then punched or printed three holes in the paper or envelope to represent the united institutions. As the New Institute press release stated, \"With Martens\u2019 design, history is not only visible but it is also literally given a new layer, symbolizing a new mission and ambition.\" From the New Institute identity campaign, Mr. Martens is donating eighteen pieces. This campaign provides a nice process story, since it demonstrates the transition from the logo material prepared by Bruce Mau to the New Institute, allowing us to see how a company or non-profit adjusts a prior graphic design campaign given a change in mission or circumstance.",
    "gallery_text": null,
    "label_text": null,
    "videos": null,
    "on_display": null,
    "woe:country": "23424909",
    "type": "Note card",
    "images": [
        {
            "b": {
                "url": "https:\/\/images.collection.cooperhewitt.org\/87603_ae15c6c8f5c06f3d_b.jpg",
                "width": 511,
                "height": 1024,
                "is_primary": "1",
                "image_id": "87603"
            },
            "z": {
                "url": "https:\/\/images.collection.cooperhewitt.org\/87603_ae15c6c8f5c06f3d_z.jpg",
                "width": 319,
                "height": 640,
                "is_primary": "1",
                "image_id": "87603"
            },
            "n": {
                "url": "https:\/\/images.collection.cooperhewitt.org\/87603_ae15c6c8f5c06f3d_n.jpg",
                "width": 160,
                "height": 320,
                "is_primary": "1",
                "image_id": "87603"
            },
            "d": {
                "url": "https:\/\/images.collection.cooperhewitt.org\/87603_ae15c6c8f5c06f3d_d.gif",
                "width": 160,
                "height": 320,
                "is_primary": "1",
                "image_id": "87603"
            },
            "sq": {
                "url": "https:\/\/images.collection.cooperhewitt.org\/87603_ae15c6c8f5c06f3d_sq.jpg",
                "width": 300,
                "height": 300,
                "is_primary": "1",
                "image_id": "87603"
            }
        }
    ],
    "participants": [
        {
            "person_id": "18064377",
            "role_id": "35351535",
            "person_name": "Karel Martens",
            "person_date": "Dutch, b. 1939",
            "role_name": "Donor",
            "role_display_name": "Donated by",
            "person_url": "http:\/\/collection.cooperhewitt.org\/people\/18064377\/",
            "role_url": "http:\/\/collection.cooperhewitt.org\/roles\/35351535\/"
        },
        {
            "person_id": "18064377",
            "role_id": "35236655",
            "person_name": "Karel Martens",
            "person_date": "Dutch, b. 1939",
            "role_name": "Designer",
            "role_display_name": "Designed by",
            "person_url": "http:\/\/collection.cooperhewitt.org\/people\/18064377\/",
            "role_url": "http:\/\/collection.cooperhewitt.org\/roles\/35236655\/"
        }
    ],
    "tombstone": {
        "epitaph": "Note Card, New Institue Flyer, 2013. Offset lithograph on paper. \nGift of Karel Martens. 2014-2-4."
    },
    "colors": [

    ],
    "woe:country_name": "Netherlands"
}
public class Object {
    private int id;
    private String title;
    private String date;
    private List<Participants> participants;

    public int getId() {
           return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public List<Participants> getParticipants() {
        return participants;
    }

    public void setParticipants(List<Participants> participants) {
        this.participants = participants;
    }
}
public class Participants {

    private String person_name;

    public String getPerson_name() {
        return person_name;
    }

    public void setPerson_name(String person_name) {
        this.person_name = person_name;
    }
}
{
    "id": "68245603",
    "title": "Note Card, New Institue Flyer, 2013",   
    "date": "2013",   
    "participants": [
        {
            "person_name": "Karel Martens"
        },
        {
            "person_name": "Karel Martens"
        }
    ]
}
Object objects = new ObjectMapper().readValue(jsonFIle, Object.class);