简体   繁体   English

Jackson Json:将Json Tree节点转换为Java对象并将其添加到ArrayList

[英]Jackson Json: Converting a Json Tree Node to an Java Object and add it to an ArrayList

I'm still new to Java and parsing Json. 我还是Java和解析Json的新手。 I'm trying to build a Comic Webapp with Spring. 我正在尝试使用Spring构建Comic Webapp。 The Database is a Json File, which holds an Array of different Comics. 数据库是一个Json文件,其中包含一系列不同的Comics。 I wanted to convert the Json Array to Java Objects and put it into an ArrayList but I seem to make a mistake somewhere along the way. 我想将Json Array转换为Java Objects并将其放入ArrayList,但是在此过程中我似乎犯了一个错误。 Maybe you can tell me what I'm doing wrong? 也许你可以告诉我我在做什么错? While doing a JUnit Test I get the following error: 在执行JUnit测试时,出现以下错误:

Error com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Comic" (class de.uni_koeln.comics.data.Comic), not marked as ignorable (6 known properties: , "title", "issue", "id", "box", "publisher", "comment"])
 at [Source: N/A; line: -1, column: -1] (through reference chain: de.uni_koeln.comics.data.Comic["Comic"])

Comic.class Comic.class

package de.uni_koeln.comics.data;

import de.uni_koeln.comics.service.JsonImportService;

public class Comic {
    private JsonImportService jservice;

public String title;
public int id;
public int issue;
public int box;
public String publisher;
public String comment;

public Comic() {

}
public Comic(String title, int issue, int box, String publisher, String   comment) {
        this.title = title;
        this.issue = issue;
        this.box = box;
        this.comment = comment;
        this.publisher = publisher;
    }

//setter and getter

` JsonImportService `JsonImportService

package de.uni_koeln.comics.service;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


@Service
public class JsonImportService {
    private List<Comic> comicList;

    @Test
    public void readComicJson() throws JsonParseException, JsonMappingException, IOException {

        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(new File("src/main/resources/Comics.json"));
    Comic comic = mapper.treeToValue(root, Comic.class);

    JsonNode contactNode = root.path("Comic");
    if (contactNode.isArray()) {
        for (JsonNode node : contactNode) {

            String title = node.path("Title").asText();
            int issue = node.path("Issue #").asInt();
            int box = node.path("Box #").asInt();
            String publisher = node.path("Publisher").asText();
            String comment = node.path("Comments").asText();

            comic.setTitle(title);
            comic.setIssue(issue);
            comic.setBox(box);
            comic.setPublisher(publisher);
            comic.setComment(comment);
            comicList.add(new Comic(title,box,issue,publisher,comment));
        }
    }
}

public List<Comic> getComicList() {
    return comicList;
}

Try to use the @JsonIgnoreProperties annotation like blow. 尝试使用@JsonIgnoreProperties批注,如blow。

@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)

public class Comic { 公共课漫画{

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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