[英]Deserializing nested json object with Jackson
我想問你們如何用傑克遜反序列化嵌套對象。 我有示例Json File:
{
"id": "1",
"comment": "Some comment",
"user": "Smith",
"date": "2018-05-31",
"shape": "oval",
"coordinates": [ ["50", "130"], ["370", "500"] ]
},
比方說,主類是所謂的Comment
,但我想創建另一個類Coordinates
與局部變量x1, x2, y1, y2
。 所以類Comment
看起來像這樣:
public class CommentFile implements Serializable{
private Long id;
private String comment;
private String user;
private String date;
private String shape;
private Coordinates coordinates;
//setters, getters, constructor
但是因為在json中我有'array' "coordinates": [ ["50", "130"], ["370", "500"] ]
,所以我不知道如何將其轉換為:
public class Coordinates implements Serializable{
private double x1;
private double y1;
private double x2;
private double y2;
有任何想法嗎?
一種簡單的方法是使用List<List<String>> coordinates;
首先將JSON反序列化為Java。 稍后,您可以在CommentFile
類中實現類似於getCoordinatesObject()
的方法來創建和獲取Coordinates對象。
也許有更好的方法直接與Jackson一起完成此操作,但是您也可以這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.