我有兩個jsons合併JSON對象
1.
{"root": {
"item": [
{
"groupName": "Al Karama Fire Station",
"vehicleId": 211,
"speed": 81
},
{
"groupName": "Al Karama Fire Station",
"vehicleId": 137,
"speed": 83
}
],
"rowCount": 2
}}
2.
{"root": {
"item": {
"groupName": "Al Karama Fire Station",
"vehicleId": 222,
"speed": 1
},
"rowCount": 1
}}
理想的第二JSON應該給我 「項」 的列表,但它沒有發生。我需要轉換合併兩個JSON來獲得一個合併。
預計:
{"root": {
"item": [
{
"groupName": "Al Karama Fire Station",
"vehicleId": 211,
"speed": 81
},
{
"groupName": "Al Karama Fire Station",
"vehicleId": 137,
"speed": 83
},
{
"groupName": "Al Karama Fire Station",
"vehicleId": 222,
"speed": 1
}
],
"rowCount": 3
}}
要做到這一點,我創建了下面的DTO。
public class ParentDTO {
private RootDTO root;
public RootDTO getRoot() {
return root;
}
public void setRoot(RootDTO root) {
this.root = root;
}
}
public class RootDTO {
private List<ItemDTO> item = new ArrayList<ItemDTO>();
private Integer rowCount;
public List<ItemDTO> getItem() {
return item;
}
public void setItem(List<ItemDTO> item) {
this.item = item;
}
public Integer getRowCount() {
return rowCount;
}
public void setRowCount(Integer rowCount) {
this.rowCount = rowCount;
}
}
public class ItemDTO {
private String groupName;
private String vehicleId;
private Long speed;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getVehicleId() {
return vehicleId;
}
public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}
public Long getSpeed() {
return speed;
}
public void setSpeed(Long speed) {
this.speed = speed;
}
}
我的問題是,在合併,我越來越
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 2 column 14 path $.root.item
如果我能得到如下的第二JSON,代碼工作正常。
{"root": {
"item": [
{
"groupName": "Al Karama Fire Station",
"vehicleId": 222,
"speed": 1
}
],
"rowCount": 1
}}
請建議。
代碼來解析JSON
Gson gson = new Gson();
ParentDTO updatedloginDto2 = gson.fromJson(tmp1, ParentDTO .class);
可以改爲從(1個元件)陣列也生成的第二JSON? – crigore
這是來自外部服務 –
好吧,那是我的猜測。所以,我認爲你更好的選擇是做類似答案1的事情。對不起。 – crigore