1

I am doing a program that automatically deserializes the JSON String that I have been given, the problem is that I receive the JSON and ObjectMapper It is not able to do this in Java Object.

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

The program show this Exception:

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

I have the classes defined as I would define them in 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);
}

}

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
MUIÑOS
  • 13
  • 1
  • 5
  • do you need java class to deserealize your json in accordance with this structure? – Vault23 Nov 12 '19 at 12:05
  • Could it be that your input is truncated somehow? The error message points in that direction, see also https://stackoverflow.com/questions/36720168/could-not-read-json-unexpected-end-of-input-in-field-name – Sebastian Heikamp Nov 12 '19 at 12:38
  • I have noticed that the error is in the History since if I put 2 objects deserializes me but if I put more than 2 objects I get that error. And I have a a java class to deserialize the JSON – MUIÑOS Nov 12 '19 at 13:25

1 Answers1

1

Assuming you need to convert your JSON String to List of objects:

private ObjectMapper objectMapper = new ObjectMapper();
List<Trazado> trazados = objectMapper.readValue(json , new TypeReference<List<Trazado>>(){});
Sanket Patel
  • 227
  • 1
  • 14
  • 1
    This has helped me, thanks. Also say that the problem was lodged in the input Buffer of the bits because it was very small – MUIÑOS Nov 13 '19 at 09:06