简体   繁体   English

如何替换 JSON arrays 数组中的 NULL 值

[英]How to replace NULL values inside an Array of JSON arrays

I have not been able to find a solution to this online.我一直无法在网上找到解决方案。 I have the following JSON.我有以下 JSON。

[{
 "Name":"Adam",
"Age":"29",
"Addr":"Roland st",
"Height":"180",
"Kids": ["Charlie"]
"DummyData":
[{
    "DummyData1":null,
    "DummyData2":"840",
    "DummyData3":
    [{
        "DummyData4":"INQU",
        "DummyData5":null,
        "DummyData6":null,
        "DummyData7":123.45,
    }]
}]  }]

This JSON structure may have different attributes in the future.这个JSON结构体以后可能会有不同的属性。 Likewise it might also have different null values including in the Kids element (can have multiple single elements or null eg, "Kids": ["Name", ""] ).同样,它也可能有不同的 null 值,包括在 Kids 元素中(可以有多个单个元素或 null,例如"Kids": ["Name", ""] )。 The above is just an example.以上只是一个例子。

I want to replace all Null values with empty strings, and then return the JSON Array as a String.我想用空字符串替换所有 Null 值,然后将 JSON 数组作为字符串返回。

This is what I have so far.这是我到目前为止所拥有的。

private void traverse(JSONArray jsonArray) {

    for (int i = 0; i < jsonArray.length(); i++) {

        if(jsonArray.optJSONObject(i) != null) { //is null at "Kids": ["Charlie"]

            JSONObject objects = jsonArray.getJSONObject(i);
            Iterator<String> key = objects.keys();

            while (key.hasNext()) {
                String k = key.next().toString();
                Object innerObject = objects.get(k);

                if (innerObject instanceof JSONArray) {
                    JSONArray arr = objects.getJSONArray(k);
                    traverse(arr);

                } 
                else if (!objects.isNull(k)) {
                    if (innerObject instanceof Integer) {
                        System.out.println("Key : " + k + ", value : " + objects.getInt(k));}
                    else if (innerObject instanceof String) {
                        System.out.println("Key : " + k + ", value : " + objects.getString(k));
                    } else if (innerObject instanceof Boolean) {
                        System.out.println("Key : " + k + ", value : " + objects.getBoolean(k));
                    } else if (innerObject instanceof Double) {
                        System.out.println("Key : " + k + ", value : " + objects.getDouble(k));
                    } else if (innerObject instanceof Long) {
                        System.out.println("Key : " + k + ", value : " + objects.getLong(k));
                    }
                } 
                else {
                    System.out.println("NULL value found!!!");
                    
                }
            }
            System.out.println("-----------");
        }
    }
}

As you can see I have already been able to traverse the entire json tree.如您所见,我已经能够遍历整个 json 树。 However I have not been able to figure out how I can replace the null values in such a way that the structure of the json does not change?但是我一直无法弄清楚如何以 json 的结构不改变的方式替换 null 值? Help would be appreciated.帮助将不胜感激。 Thanks in advance.提前致谢。

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

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