gpt4 book ai didi

java - 反序列化 JSON 数据 - 使用 Jackson JSON Parser 自定义反序列化

转载 作者:行者123 更新时间:2023-11-30 03:25:50 24 4
gpt4 key购买 nike

我正在尝试反序列化以下传入的 JSON 数据:

{"TimeTable":[{"PersonID":"100649771",
..,..,..,"xx":null},
{"PersonID":"100631701",
..,..,..,"xx":{"abc":1234,"xyz":5678}}],
"xxx":"","xxxx":0,"xxxxx":false}

但是我在使用由以下内容组成的自定义反序列化 block 进行解析时遇到了问题:

    jParser.nextToken();
while ((jParser.nextToken() != JsonToken.END_ARRAY)) {
String innerField = jParser.getCurrentName();
jParser.nextToken();

但是通过这种方式,我在解析数组中的第二行时跳过了数组内容(如上面的 JSON 示例所示^)。

更新:这是method (PasteBin Link) 试图解析以所述格式出现的 JSON 数据。有没有办法可以将 JSON 数据直接绑定(bind)到我的 bean? (IMO 因为 JSON 结构,它对我来说显得更复杂;此外,我无法更改 JSON 结构或 bean 结构。所以,我只是放弃了直接绑定(bind)的想法:|)无论如何 here (PasteBin Link) 也是 bean。

以下是传入 JSON 数据的示例:

{"Schedules":[{"PersonID":"100649771",
"HasSchedule":false,
"TripType":null,
"StickerNumber":null,
"VehicleRegNo":null,
"ExpectedStartDate":null,
"ActualStartDate":null,
"ActualEndDate":null,
"PersonScheduledDate":null,
"Shift":null,
"ColdCall":null,
"PickupLocationCoord":null},
{"PersonID":"100631701",
"HasSchedule":true,
"TripType":"P",
"StickerNumber":"PC0409",
"VehicleRegNo":"ASJHAHSP1758",
"ExpectedStartDate":"16 Aug 2013, 10:00:00",
"ActualStartDate":"16 Aug 2013, 10:02:52",
"ActualEndDate":"16 Aug 2013, 14:14:12",
"PersonScheduledDate":null,
"Shift":"02:30 PM",
"ColdCall":"N",
"PickupLocationCoord":{"Latitude":92.01011101,"Longitude":48.01011101}}],
"ErrorMessage":"","ErrorCode":0,"HasError":false}

任何人都可以在这里为我提供一些指导,以便正确反序列化它们吗?谢谢

最佳答案

更新

除其他事项外,您混合了两种方法来读取 JSON 流:使用 readTree() 获取内存树中的所有 JSON 数据(如 XML 的 DOM),但也使用JsonParser 逐个 token 读取 JSON 流 token (如 XML 的 JAX)。以下是使用 readTree() 执行几乎相同的方法,这似乎更适合您,因为您正在读取已加载到 String 中的 JSON:

public List<VehicleInformationBean> getAllVehiclesInTree(String response) {

List<VehicleInformationBean> vehicleList = new ArrayList<VehicleInformationBean>();

try {
PersonInformationBean mPersonInformationBean;
DatabaseHelper mDatabaseHelper = DatabaseHelper.getInstance(sContext);

ObjectMapper mapper = new ObjectMapper();
ObjectNode root = (ObjectNode) mapper.readTree(response);

if ((root.get(ServiceConstant.ErrorCode).asInt()) != 0 || !root.has(ServiceConstant.Schedules)) {
return vehicleList;
}

for(JsonNode element: root.get(ServiceConstant.Schedules)) {
VehicleInformationBean lstVehicleInformation = new VehicleInformationBean();
if (element.has(ServiceConstant.PersonID)) {
String personId = element.get(ServiceConstant.PersonID).asText();
mPersonInformationBean = mDatabaseHelper.getPersonDetailById(personId);
lstVehicleInformation.setPersonID(personId);
lstVehicleInformation.setName(mPersonInformationBean.getName());
lstVehicleInformation.setPickupLocation(mPersonInformationBean.getPickupLocation());
lstVehicleInformation.setDropLocation(mPersonInformationBean.getDropLocation());
}
lstVehicleInformation.setTripType(element.get(ServiceConstant.TripType).textValue());
lstVehicleInformation.setStickerNumber(element.get(ServiceConstant.StickerNumber).textValue());
lstVehicleInformation.setVehicleRegNo(element.get(ServiceConstant.VehicleRegNo).textValue());
lstVehicleInformation.setExpectedStartDate(element.get(ServiceConstant.ExpectedStartDate).textValue());
lstVehicleInformation.setActualStartDate(element.get(ServiceConstant.ActualStartDate).textValue());
lstVehicleInformation.setActualEndDate(element.get(ServiceConstant.ActualEndDate).textValue());
lstVehicleInformation.setPersonScheduledDate(element.get(ServiceConstant.PersonScheduledDate).textValue());
lstVehicleInformation.setShift(element.get(ServiceConstant.Shift).textValue());
if (element.has("PickupLocationCoord")) {
JsonNode coords = element.get("PickupLocationCoord");
if(coords.has(ServiceConstant.Latitude)) {
lstVehicleInformation.setLatitude(coords.get(ServiceConstant.Latitude).asDouble());
}
if(coords.has(ServiceConstant.Longitude)) {
lstVehicleInformation.setLongitude(coords.get(ServiceConstant.Longitude).asDouble());
}
} else if (element.has(ServiceConstant.ColdCall)) {
lstVehicleInformation.setColdCall(element.get(ServiceConstant.ColdCall).textValue());
}
vehicleList.add(lstVehicleInformation);
}

} catch (Exception e) {
// TODO doing something with exception or throw it if it can't be handled here
e.printStackTrace();
}
return vehicleList;
}

您需要向此方法添加一些验证和额外代码,以使其完全执行原始方法的操作。此方法仅向您展示了如何操作的主要思路。

关于java - 反序列化 JSON 数据 - 使用 Jackson JSON Parser 自定义反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com