简体   繁体   中英

Deserialize JSON document to Java Objects (error with an arraylist)

I've managed to get many fields (id, title, date, etc) from the json files I'm working with but when it comes to the participants field, i can't seem to get it working. How do i get the participants arraylist working along with the other fields? Heres my code:

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.");
        }

    }

}

Run Configurations > Argument (link to json files)

/Users/mycomputer/Documents/JavaWorkspace/git/collection/objects/682/456

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


}

Console Output

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

Example json file

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

If I understood your question correctly, you just need to define the list of participants in your Objects class. I don't see in your code how you defined the participants, and the error you get.

A quick example for you, having the class 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;
    }
}

And the class 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;
    }
}

For the JSON file:

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

The code:

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

will populate your Object with participants as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM