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

时间:2013-08-16 12:00:03

标签: java android json jackson

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

更新这是method(PasteBin链接),它试图解析以所述格式出现的JSON数据。有没有办法可以将JSON数据直接绑定到我的bean? (由于JSON结构,IMO对我来说似乎更复杂;而且我不能改变JSON结构和bean结构。所以,我只是放弃了直接绑定的想法:|)无论如何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}

请有人可以在这里为我指点一些指示 - 正确地反序列化它们吗? 感谢

1 个答案:

答案 0 :(得分:1)

已更新

除此之外,您正在混合使用两个aproaches来读取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;
}

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