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

时间:2015-03-29 15:18:23

标签: java json git arraylist

我设法从我正在使用的json文件中获取了许多字段(id,title,date等)但是当涉及到参与者字段时,我似乎无法让它工作。我如何让参与者arraylist与其他领域一起工作?继承我的代码:

Application.java

package mypackage;

import mypackage.Objects;
import mypackage.Participation;
import mypackage.Participant;
import mypackage.Role;
import mypackage.utility.FileFinder;

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Application implements Serializable{

    static final long serialVersionUID = 1L;

    public static void main(String[] args) {

        try {

            //Run configuration 'parse' - finds all objects within 682/456 - 21 in total 
            String objectsFile = args[0];

            List<Path> files1 = FileFinder.getFileList(objectsFile, "*.json");
                for (Path f : files1) {
                    Objects objects = new ObjectMapper().readValue(f.toFile(), Objects.class);
                    System.out.printf("Processing Object file: %s...", objectsFile);
                    System.out.println(objects.toString() + "\n" + 
                              "-------------------------------------------------------------------"
                            + "-------------------------------------------------------------------" );
                }

        } 

          catch (JsonParseException e) {
            System.out.println("Error parsing the file.");
        } catch (JsonMappingException e) {
            System.out.println("Error mapping to Java object.");
        } catch (IOException e) {
            System.out.println("Unknown I/O error.");
        }

    }

}

运行配置&gt;参数(链接到json文件)

/用户/ MYCOMPUTER /文档/ JavaWorkspace / GIT中/收集/对象/456分之682

objects.java

package mypackage;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

public class Objects {

    private int id;
    private String title;
    private String date;
    private String medium;
    private String creditline;
    private String description;
    private String gallery_text;

        @Override
        public String toString() {
            String objectsAsString =      "\nID: " + id 
                                        + "\nTitle: " + title 
                                        + "\nDate: " + date 
                                        + "\nMedium: " + medium
                                        + "\nCredit: " + creditline
                                        + "\nDescription: " + description
                                        + "\nGallery Text: " + gallery_text
                                        ;
            return objectsAsString;
        }

            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 String getMedium() {
                return medium;
            }

            public void setMedium(String medium) {
                this.medium = medium;
            }

            public String getCreditline() {
                return creditline;
            }

            public void setCreditline(String creditline) {
                this.creditline = creditline;
            }

            public String getDescription() {
                return description;
            }

            public void setDescription(String description) {
                this.description = description;
            }

            public String getGallery_text() {
                return gallery_text;
            }

            public void setGallery_text(String gallery_text) {
                this.gallery_text = gallery_text;
            }


}

控制台输出

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

示例json文件

{
    "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"
}

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您只需要定义Objects课程中的参与者列表。我在你的代码中没有看到你如何定义参与者,以及你得到的错误。

快速举例,有一个班级Object

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;
    }
}

班级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;
    }
}

对于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);

也会在参与者中填充Object