如何以编程方式将JSON转换为JSON模式以比较两个JSON对象的结构

时间:2018-09-19 12:39:29

标签: java json

如何以编程方式将JSON转换为JSON模式以比较Json Structure,而无需 考虑值。例如,我提到了两个json

{ “ name”:“ John”, “年龄”:30岁, “地址”:伦敦 }    和

{ “ name”:“ Robert”, “年龄”:31, “地址”:Southampton }

我已经写了如下代码。方法应该返回true但返回false。因为两个json属性值不同,所以我只想比较结构

public static boolean assertXmlStructureEquals(String expectedXML,String actualXML) {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
            Node doc = docBuilder.parse(expectedXML);
            Node doc1 = docBuilder.parse(actualXML);
            XpathFormatter expectedXMLFormatter = new XpathFormatter(doc);
            XpathFormatter actualXMLFormatter = new XpathFormatter(doc1);
            if (expectedXMLFormatter.equals(actualXMLFormatter)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }

    }

0 个答案:

没有答案