我有一个源JSON字符串,我想将其转换为本地属性的JSON。我正在努力实现:
{
"dep": 1552928400000,
"des": "USA",
"listWagonDetail": [
{
"listSeatDetail": [
{
"seatColumn": "A",
"seatRow": "1",
"seatStatus": "1",
"subclass": "C"
},
{
"seatColumn": "A",
"seatRow": "2",
"seatStatus": "0",
"subclass": "C"
},
{
"seatColumn": "A",
"seatRow": "3",
"seatStatus": "1",
"subclass": "C"
}
],
"numSeat": 0,
"wagonCode": "PRE",
"wagonNo": "1"
},
{
"listSeatDetail": [
{
"seatColumn": "A",
"seatRow": "1",
"seatStatus": "0",
"subclass": "C"
},
{
"seatColumn": "A",
"seatRow": "2",
"seatStatus": "0",
"subclass": "C"
},
{
"seatColumn": "A",
"seatRow": "3",
"seatStatus": "1",
"subclass": "C"
}
],
"numSeat": 0,
"wagonCode": "PRE",
"wagonNo": "2"
}
],
"numWagon": 0,
"org": "AU",
"subClass": null,
"trainNo": "77"
}
将每个listSeatDetail按WagonNo分组。但是相反,我在工作中将“ numSeat”,“ wagonCode”和“ WagonNo”循环到每个listSeatDetail上,并且仅显示每个数组的最后一个数据:
{
"dep": 1552928400000,
"des": "USA",
"listWagonDetail": [
{
"listSeatDetail": [
{
"seatColumn": "A",
"seatRow": "3",
"seatStatus": "1",
"subclass": "C"
}
],
"numSeat": 0,
"wagonCode": "PRE",
"wagonNo": "2"
},
{
"listSeatDetail": [
{
"seatColumn": "A",
"seatRow": "3",
"seatStatus": "1",
"subclass": "C"
}
],
"numSeat": 0,
"wagonCode": "PRE",
"wagonNo": "2"
},
{
"listSeatDetail": [
{
"seatColumn": "A",
"seatRow": "3",
"seatStatus": "1",
"subclass": "C"
}
],
"numSeat": 0,
"wagonCode": "PRE",
"wagonNo": "2"
},
如何获得第一个JSON?这是我的代码的一部分:
private void generateSeat(GetSeatRequest request, GetSeatMapResponse seatMapResponse,
SeatMapResponsePojo seatmapResponsePojo) {
seatMapResponse.setDepDate(request.getDepDate());
seatMapResponse.setDes(request.getDes());
seatMapResponse.setOrg(request.getOrg());
seatMapResponse.setTrainNo(request.getTrainNo());
seatMapResponse.setNumWagon(0);
seatMapResponse.setSubClass(request.getSubClass());
List<WagonDetail> listWagonDetail = new ArrayList<>();
for (SeatMapPojo seatmap : seatmapResponsePojo.getPayload()) {
WagonDetail wagon = new WagonDetail();
String wagonNoString = seatmap.getStamformdetcode();
String[] wagonNo = wagonNoString.split("-");
for (WagonDetail wagondetail : listWagonDetail) {
wagondetail.setNumSeat(0);
wagondetail.setWagonCode(wagonNo[0]);
wagondetail.setWagonNo(wagonNo[1]);
List<SeatDetail> listSeatDetail = new ArrayList<>();
SeatDetail seatDetail = new SeatDetail();
seatDetail.setSeatColumn(seatmap.getWagondetcol());
seatDetail.setSeatRow(seatmap.getWagondetrow());
seatDetail.setSeatStatus(seatmap.isIsavilable() ? "0" : "1");
seatDetail.setSubclass(seatmap.getSubclass());
listSeatDetail.add(seatDetail);
wagondetail.setListSeatDetail(listSeatDetail);
}
listWagonDetail.add(wagon);
}
seatMapResponse.setListWagonDetail(listWagonDetail);
}
答案 0 :(得分:0)
我建议您使用google Gson,这是迄今为止我们发现的最好的JSON序列化/反序列化库。
您可以像这样定义所需的类:
class Output{
private int depDate;
private String des;
...
private List<WagonDetail> listWagonDetail;
//... getter()/setter()
}
然后您可以定义WagonDetail
:
class WagonDetail{
private List<SeatDetail> listSeatDetail;
private int numSeat;
....
}
然后再上SeatDetail
类,
您可以使用gson非常轻松地序列化该类Output:
Output output = new Output();
//set according attributes using setter/getter
Gson gson = new Gson();
String jsStr = gson.toJson(output);
将JSON字符串反序列化为对象也很容易:
String jsStr = "..."
Gson gson = new Gson();
Output output = gson.fromJson(jsStr, Output.class);