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



我正在尝试反序列化以下传入的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示例^所示)。

UPDATE这是一个方法(PasteBin-Link),它试图解析所描述格式的JSON数据。有没有一种方法可以将JSON数据直接绑定到我的bean?(在我看来,由于JSON结构的原因,它看起来更复杂;此外,我既不能更改JSON结构,也不能更改bean结构。因此,我直接放弃了绑定的想法:|)这里的Anyways(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逐个令牌读取JSON流(如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;
}

您需要向该方法添加一些验证和额外的代码,使其能够像您的原始方法那样执行。这个方法只向您展示了如何做到这一点的主要思想。

最新更新