xml和json的判断格式是否相等

 

手动撸了个 xml 和 json 格式匹配的代码。

 

    /**
     * 判断数据的结构。遍历节点然后去对应的json那边查找是否存在这样的节点
     *
     * @param root       配置的xml所对应的对Element对象
     * @param jsonObject 需要匹配的json字符串对应的json对象
     * @return
     */
    private boolean constructionCompared(Element root, JSONObject jsonObject) {
        boolean f = true;
        List<Element> elementList = root.elements();
        for (Element element : elementList) {
            String key = element.getName();
            Object o = jsonObject.get(key);
            if (o == null) {
                return false;
            }
            if (o instanceof JSONObject) {
                JSONObject t = (JSONObject) o;
                f = f && constructionCompared(element, t);
                if (f == false) {
                    return false;
                }
            } else if (o instanceof JSONArray) {
                JSONArray t = (JSONArray) o;
                if (t.size() == 0 && element.elements().size() > 0) {
                    return false;
                }
                f = f && constructionCompared(element, t.getJSONObject(0));
                if (f == false) {
                    return false;
                }
            } else {
                if (element.elements().size() > 0) {
                    return false;
                }
            }
        }
        return f;
    }

 

测试数据:

json:

{
    "unid":"F92933BF937B70976FB1DA330059F7B6",
    "pass_device_unid":"41ACB22E9F0D131EFB97C382503E60F6",
    "pass_area_unid":"0D77703E4AD042A6FA59807746331821",
    "pass_bay_unid":"7AD94DCE176EB10602938BD164E156EC",
    "pass_plate_no":"闽C3285W",
    "pass_plate_type":"02",
    "pass_datetime":"2019-05-26 08:33:10",
    "pass_car_speed":"0.0",
    "pass_car_length":"0.0",
    "pass_plate_color":"H",
    "pass_car_type":"K33",
    "pass_pic_full_path":"35050001/F92933BF937B70976FB1DA330059F7B61",
    "pass_pic_path2":"",
    "pass_pic_path3":"",
    "pass_car_status":"0",
    "pass_car_color":"Z",
    "pass_car_brand":"",
    "pass_capture_no":"",
    "pass_sec_code":"",
    "pass_car_dw_index":"40424",
    "pass_car_flag":"0",
    "pass_create_time":"2019-05-26 08:34:53",
    "pass_road_no":"1",
    "pass_car_son_brand":"其他",
    "pass_car_pic_site":"",
    "pass_car_model":"",
    "pass_road_to":"0",
    "pass_identify_direction":"",
    "illegal_code":"0",
    "pass_deck":"0"
}

 

xml:

<root>
  <unid>F92933BF937B70976FB1DA330059F7B6</unid>
  <pass_device_unid>41ACB22E9F0D131EFB97C382503E60F6</pass_device_unid>
  <pass_area_unid>0D77703E4AD042A6FA59807746331821</pass_area_unid>
  <pass_bay_unid>7AD94DCE176EB10602938BD164E156EC</pass_bay_unid>
  <pass_plate_no>闽C3285W</pass_plate_no>
  <pass_plate_type>02</pass_plate_type>
  <pass_datetime>2019-05-26 08:33:10</pass_datetime>
  <pass_car_speed>0.0</pass_car_speed>
  <pass_car_length>0.0</pass_car_length>
  <pass_plate_color>H</pass_plate_color>
  <pass_car_type>K33</pass_car_type>
  <pass_pic_full_path>35050001/F92933BF937B70976FB1DA330059F7B61</pass_pic_full_path>
  <pass_pic_path2></pass_pic_path2>
  <pass_pic_path3></pass_pic_path3>
  <pass_car_status>0</pass_car_status>
  <pass_car_color>Z</pass_car_color>
  <pass_car_brand></pass_car_brand>
  <pass_capture_no></pass_capture_no>
  <pass_sec_code></pass_sec_code>
  <pass_car_dw_index>40424</pass_car_dw_index>
  <pass_car_flag>0</pass_car_flag>
  <pass_create_time>2019-05-26 08:34:53</pass_create_time>
  <pass_road_no>1</pass_road_no>
  <pass_car_son_brand>其他</pass_car_son_brand>
  <pass_car_pic_site></pass_car_pic_site>
  <pass_car_model></pass_car_model>
  <pass_road_to>0</pass_road_to>
  <pass_identify_direction></pass_identify_direction>
  <illegal_code>0</illegal_code>
  <pass_deck>0</pass_deck>
</root>

 

调用:

public void dataCheckTest(Map<String, Object> params) throws Exception {
        String xml = String.valueOf(params.get("xml"));
        String json = String.valueOf(params.get("json"));
        JSONObject jsonObject = JSONObject.parseObject(json);
        Document document = DocumentHelper.parseText(xml);
        Element root = document.getRootElement();
        boolean b = constructionCompared(root, jsonObject);
        System.out.println(b);
    }

 

posted @ 2019-06-11 11:18  护花使者  Views(537)  Comments(0Edit  收藏  举报