简体   繁体   中英

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.

[{
 "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. Likewise it might also have different null values including in the Kids element (can have multiple single elements or null eg, "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.

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. 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? Help would be appreciated. Thanks in advance.

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