繁体   English   中英

JSON文件中Java中的键值对

[英]Key value pair in Java from JSON file

我有一个JSON数据

{
"HiServiceInquiryResponse": {
    "CoverageInfoResponse": {
        "Participant": {
            "ns1:PersonalInfo": {
                "ns1:LastName": "AA",
                "ns1:Address": [
                    {
                        "ns1:Province": "",
                        "ns1:State": "CA",
                        "ns1:City": "LOS ANGELES",
                        "ns1:Country": "US",
                        "ns1:Address2": "",
                        "ns1:Address1": "test",
                        "ns1:PostalCode": 12345
                    },
                    {
                        "ns1:Province": "",
                        "ns1:State": "CA",
                        "ns1:City": "LOS ANGELES",
                        "ns1:Country": "US",
                        "ns1:Address2": "",
                        "ns1:Address1": "test",
                        "ns1:PostalCode": 12345
                    }
                ],
                "ns1:FirstName": "BB"
            },
            "ns1:Coverage": "",
            "ns1:HiClientId": 57,
            "ns1:Employment": {
                "ns1:EmployeeId": 1234,
                "ns1:TaxId": 111
            }
        }
    }
}

}

我想读取所有键值对并存储它们。 到目前为止,我已经能够做到

    public static void printJsonObject(JSONObject jsonObj) {

    for (Object key : jsonObj.keySet()) {
        String keyStr = (String) key;
        Object keyvalue = jsonObj.get(keyStr);

        if (!(keyvalue instanceof JSONObject)) {
            System.out.println(keyStr + ", " + keyvalue);
        }
        if (keyvalue instanceof JSONObject) {
            printJsonObject((JSONObject) keyvalue);
        }
    }
}

问题是,当我们在personInfo中有2个地址时,它不会单独读取它们。

只有1个地址时的“我的输出”:->

    ns1:LastName, AA
    ns1:Province, 
    ns1:State, CA
    ns1:City, LOS ANGELES
    ns1:Country, US
    ns1:Address2, 
    ns1:Address1, test
    ns1:PostalCode, 12345
    ns1:FirstName, BB
    ns1:Coverage, 
    ns1:HiClientId, 57
    ns1:EmployeeId, 1234
    ns1:TaxId, 111

我的输出中有2个地址时:->

    ns1:LastName, AA
    ns1:Address, [{"ns1:Province":"","ns1:State":"CA","ns1:City":"LOS ANGELES","ns1:Country":"US","ns1:Address2":"","ns1:Address1":"test","ns1:PostalCode":12345},{"ns1:Province":"","ns1:State":"CA","ns1:City":"LOS ANGELES","ns1:Country":"US","ns1:Address2":"","ns1:Address1":"test","ns1:PostalCode":12345}]
    ns1:FirstName, BB
    ns1:Coverage, 
    ns1:HiClientId, 57
    ns1:EmployeeId, 1234
    ns1:TaxId, 111

我希望同时显示两个地址的数据。

要解析JSONObject内部的数组,您必须检查JSONArray值实例,并递归地为每个数组项调用printJsonObject

public static void printJsonObject(JSONObject jsonObj) {
    for (Object key : jsonObj.keySet()) {
        Object value = jsonObj.get(key);

        if (value instanceof JSONObject)
            printJsonObject((JSONObject)value);
        else if (value instanceof JSONArray)
            ((JSONArray)value).forEach(obj -> printJsonObject((JSONObject)obj));
        else
            System.out.println(key + ", " + value);
    }
}

这应该可以解决您的问题

public static void printJsonObject(JSONObject jsonObj) {

        for (Object key : jsonObj.keySet()) {
            String keyStr = (String) key;
            Object keyvalue = jsonObj.get(keyStr);

            if (keyvalue instanceof JSONObject) {
                printJsonObject((JSONObject) keyvalue);
            } else if (keyvalue instanceof JSONArray) {
                JSONArray array = (JSONArray) keyvalue;
                for (int i = 0; i < array.length(); i++) {
                    printJsonObject((JSONObject) array.get(i));
                }

            } else {
                System.out.println(keyStr + ", " + keyvalue);
            }
        }
    }

将您的代码更改为

  if (!(keyvalue instanceof JSONObject)) {

    if(keyStr.equals("ns1:Address")){

        //now it is your array so loop through it and call printJsonObject((JSONObject) keyvalue); on each object

    }
    else{
        System.out.println(keyStr + ", " + keyvalue);
    }
    }

发生这种情况的原因是,当有两个地址时,对应于该地址的JSONObject是一个数组。 如果要单独打印,请检查它是否是InstanceArray。 然后解析数组中的不同地址。 如果它不是数组,则只打印它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM