簡體   English   中英

將JSON文檔反序列化為Java對象(arraylist錯誤)

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

我設法從正在使用的json文件中獲取了很多字段(id,標題,日期等),但是當涉及到參與者字段時,我似乎無法正常工作。 我如何讓參與者arraylist與其他字段一起工作? 這是我的代碼:

應用程序

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

    }

}

運行配置>參數(鏈接到json文件)

/用戶/ mycomputer /文檔/ 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;
            }


}

控制台輸出

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

如果我正確理解了您的問題,則只需要定義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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM