简体   繁体   中英

Key value pair in Java from JSON file

I have an JSON data

{
"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
            }
        }
    }
}

}

I want to read all the key-value pairs and store them. So far I am able to do it

    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);
        }
    }
}

The problem is when we have 2 address in the personalInfo it does not read them separately.

My Output when there is only 1 address: ->

    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

My Output when there are 2 address: ->

    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

I want the data should be displayed for both addresses.

To parse array inside JSONObject you have to check value instance of JSONArray and recursively call printJsonObject for each array item:

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);
    }
}

this should solve your problem

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);
            }
        }
    }

change your code as

  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);
    }
    }

This is happening because when there are two addresses, the JSONObject corresponding to the address is an array. If you want it to be printed separately, check whether it is an instanceOf JSONArray. Then parse through the different addresses in the array. If it is not an array, just print it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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