【问题标题】:jackson- List of objects com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input in field name?jackson- com.fasterxml.jackson.core.io.JsonEOFException 对象列表:字段名称中的输入意外结束?
【发布时间】:2020-03-08 02:25:57
【问题描述】:

我正在做一个自动反序列化我得到的 JSON 字符串的程序,问题是我收到了 JSONObjectMapper 它无法做到Java 对象中的 this。

  "Trazado": [
    {
      "Historial": [
        {
          "Longitude": 454353,
          "Latitude": 32423
        },
        {
          "Longitude": 454353,
          "Latitude": 32423
        },
        {
          "Longitude": 454353,
          "Latitude": 32423
        }
      ]
    },
    {
      "Historial": [
        {
          "Longitude": 454353,
          "Latitude": 32423
        }
      ]
    }
  ],

程序显示此异常:

com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input in field name

我已经定义了在https://www.jsonschema2pojo.org/ 中定义的类

-----------------------------------com.example.DenmJson.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DenmJson {

private List<Trazado> trazado = new ArrayList<Trazado>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* No args constructor for use in serialization
*
*/
public DenmJson() {
}

/**
*
* @param trazado
*/
public DenmJson(List<Trazado> trazado) {
super();
this.trazado = trazado;
}

public List<Trazado> getTrazado() {
return trazado;
}

public void setTrazado(List<Trazado> trazado) {
this.trazado = trazado;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Historial.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Historial {

private Integer longitude;
private Integer latitude;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* No args constructor for use in serialization
*
*/
public Historial() {
}

/**
*
* @param latitude
* @param longitude
*/
public Historial(Integer longitude, Integer latitude) {
super();
this.longitude = longitude;
this.latitude = latitude;
}

public Integer getLongitude() {
return longitude;
}

public void setLongitude(Integer longitude) {
this.longitude = longitude;
}

public Integer getLatitude() {
return latitude;
}

public void setLatitude(Integer latitude) {
this.latitude = latitude;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Trazado.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Trazado {

private List<Historial> historial = new ArrayList<Historial>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* No args constructor for use in serialization
*
*/
public Trazado() {
}

/**
*
* @param historial
*/
public Trazado(List<Historial> historial) {
super();
this.historial = historial;
}

public List<Historial> getHistorial() {
return historial;
}

public void setHistorial(List<Historial> historial) {
this.historial = historial;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

【问题讨论】:

  • 你需要java类按照这个结构去实现你的json吗?
  • 会不会是您的输入被截断了?错误消息指向那个方向,另见stackoverflow.com/questions/36720168/…
  • 我注意到错误出现在历史记录中,因为如果我放置 2 个对象会反序列化我,但如果我放置超过 2 个对象,则会出现该错误。我有一个 java 类来反序列化 JSON

标签: java json spring jackson objectmapper


【解决方案1】:

假设您需要将 JSON 字符串转换为对象列表:

private ObjectMapper objectMapper = new ObjectMapper();
List<Trazado> trazados = objectMapper.readValue(json , new TypeReference<List<Trazado>>(){});

【讨论】:

  • 这对我有帮助,谢谢。也说问题是在bits的input Buffer中,因为它非常小
猜你喜欢
  • 2016-08-11
  • 1970-01-01
  • 2021-08-22
  • 2016-08-05
  • 2018-01-23
  • 2020-07-10
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多