来自JSON文件的Java中的键值对

时间:2018-06-12 19:36:44

标签: java json

我有一个JSON数据

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

}

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

    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

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

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

    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

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

ssm = boto3.client('ssm' )
            instance = 'i-123123'
            response = ssm.send_command( InstanceIds=[ instance ], DocumentName='AWS-RunShellScript', Comment='slack testing', Parameters={ "commands":[ "hostname" ]  } )
            command_id = response['Command']['CommandId']
            command_id.encode("utf-8")
            output = ssm.get_command_invocation(
                CommandId=command_id,
                InstanceId=instance
                )

我希望两个地址都能显示数据。

4 个答案:

答案 0 :(得分:0)

这应该可以解决你的问题

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

答案 1 :(得分:0)

将您的代码更改为

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

答案 2 :(得分:0)

这种情况正在发生,因为当有两个地址时,对应于该地址的JSONObject是一个数组。如果您希望单独打印,请检查它是否是一个instanceOf JSONArray。然后解析数组中的不同地址。如果它不是数组,只需打印它。

答案 3 :(得分:0)

要解析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);
    }
}