JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例
Posted
技术标签:
【中文标题】JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例【英文标题】:JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token 【发布时间】:2018-01-21 15:57:32 【问题描述】:我在我的项目中使用 spring boot 和 spring 数据,我有两个类:
@Entity
public class Mission implements Serializable
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
private String departure;
private String arrival;
private Boolean isFreeWayEnabled;
@OneToMany( mappedBy = "mission" )
private List<Station> stations;
// getters and setters
第二类:
@Entity
public class Station implements Serializable
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
private String station;
@ManyToOne( fetch = FetchType.LAZY )
@JsonBackReference
private Mission mission;
//getters and setters
和控制器:
@RequestMapping( value = "mission/addMission", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8" )
public Reponse addMission( @RequestBody Mission mission ) throws ServletException
if ( messages != null )
return new Reponse( -1, messages );
boolean flag = false;
try
application.addMision( mission );
application.addStation( mission.getStations(), mission.getId() );
flag = true;
catch ( Exception e )
return new Reponse( 5, Static.getErreursForException( e ) );
return new Reponse( 0, flag );
问题是当我尝试使用 JSON 添加新任务时:
"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,stations:"id":1
【问题讨论】:
你能分享你想要反序列化的json吗? 以上:"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,"stations":"id":1 【参考方案1】:您正在尝试将对象反序列化为列表。您需要将 Stations 设为 JSON 数组
"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,stations:["id":1, "id":2]
【讨论】:
以上是关于JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例的主要内容,如果未能解决你的问题,请参考以下文章
JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例