1

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

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

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

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

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

更新这是尝试解析以所述格式出现的 JSON 数据的方法(PasteBin Link)。有没有办法可以将 JSON 数据直接绑定到我的 bean?(由于 JSON 结构,IMO 对我来说似乎更复杂;此外,我无法更改 JSON 结构或 bean 结构。所以,我只是放弃了直接绑定的想法:|)无论如何这里(PasteBin Link)是豆子也是。

以下是传入 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}

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

4

1 回答 1

1

更新

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

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;
}

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

于 2013-08-16T12:17:45.520 回答