java - 解析最初使用 Jackson 和 Jackson 解析器编码的 JSON 字符串会抛出 IOExeption

标签 java json parsing jackson

我创建了一个相当基本的 Java 对象,它包含三个变量;一个字符串、一个字符串[]和一个字符串[][]。使用 Jackson 将其完美编码为 JSON 对象,并且我能够打印它。然而,尝试使用 Jackson 解析此字符串不会给我返回对象,它只会抛出 IOException。

下面的代码可用于复制我的问题。

对象:

public class CityJSON {

    String[] currentCityList;
    String brokenCity;
    String[][] cityCosts;

    CityJSON(String[] ccl, String bc, String[][] cc){
        currentCityList = ccl;
        brokenCity = bc;
        cityCosts = cc;
    }

    public String[] getCurrentCityList() {
        return currentCityList;
    }

    public void setCurrentCityList(String[] currentCityList) {
        this.currentCityList = currentCityList;
    }

    public String getBrokenCity() {
        return brokenCity;
    }

    public void setBrokenCity(String brokenCity) {
        this.brokenCity = brokenCity;
    }

    public String[][] getCityCosts() {
        return cityCosts;
    }

    public void setCityCosts(String[][] cityCosts) {
        this.cityCosts = cityCosts;
    }

}

编码器和解析器:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Tester {
    public static void main(String args[]){


        String[] cities = {"A city", "Another city"};
        String[] one = {"cost1", "cost2"};
        String[] two = {"cost3", "cost4"};
        String[][] twod = new String[2][2];
        twod[0] = one;
        twod[1] = two;
        CityJSON json = new CityJSON(cities, "myCity", twod);


        String gen = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            gen = mapper.writeValueAsString(json);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(gen);


        try {
            CityJSON ob = new ObjectMapper().readValue(gen, CityJSON.class);
        } catch (IOException ex) {
            System.out.println("FAILED");
        }

    }
}

catch 语句将被触发,导致打印“FAILED”。

最佳答案

IOException 状态:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class CityJSON]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: {"currentCityList":["A city","Another city"],"brokenCity":"myCity","cityCosts":[["cost1","cost2"],["cost3","cost4"]]}; line: 1, column: 2] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)

因此,创建一个无参数构造函数,或者使用 @JsonCreator 注释现有构造函数,并使用 @JsonProperty("propertyName") 注释该构造函数的参数 - 例如

@JsonCreator
CityJSON(
    @JsonProperty("currentCityList") String[] ccl,
    @JsonProperty("brokenCity") String bc,
    @JsonProperty("cityCosts")String[][] cc
){
    currentCityList = ccl;
    brokenCity = bc;
    cityCosts = cc;
}

关于java - 解析最初使用 Jackson 和 Jackson 解析器编码的 JSON 字符串会抛出 IOExeption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35919603/

相关文章:

java - JAXB - SAXParseException 找不到元素的声明

c++ - 解析小的特定参数的最有效方法

java - 进程失败,退出代码为1

java - 通用推送到堆栈会出现编译错误

java - 无法从 View 转换为按钮错误

javascript - 如何使用 PHP 将 HTML 转换为 JSON?

javascript - NodeJS - 如何抓取 ld+json 数据并将其保存到对象中

java - Microsoft 或 Widcomm Stack 上的 Windows Mobile 蓝牙访问

json - 如何处理 JSON 中的多对多关系?

java - 如何为 ANTLR 中的模糊输入生成多个解析树