在Java中的嵌套josn对象中更改键名称

时间:2020-02-26 08:32:59

标签: java json

有人可以帮我解决这个问题吗,我有一个高度嵌套到多个级别的JSONObject,我想用不同的值替换某些键吗?以下是我的JSONOBject:

{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Put_Location_Request",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
    "nullable": null,
    "localPart": null,
    "prefix": null,
    "precision": null,
    "name": "Location_Reference",
    "namespace": null,
    "scale": null,
    "unicode": null,
    "type": null,
    "maxLength": null,
    "srid": null,
    "subEntities": [{
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "ID",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }]
}, {
    "nullable": null,
    "localPart": null,
    "prefix": null,
    "precision": null,
    "name": "Location_Data",
    "namespace": null,
    "scale": null,
    "unicode": null,
    "type": null,
    "maxLength": null,
    "srid": null,
    "subEntities": [{
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Location_ID",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Effective_Date",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Location_Name",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Location_Usage_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Location_Type_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Location_Attribute_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Superior_Location_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Inactive",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Latitude",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Longitude",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Altitude",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Time_Profile_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Locale_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Display_Language_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Time_Zone_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Default_Currency_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "External_Name",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Default_Job_Posting_Location_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Trade_Name",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Worksite_ID_Code",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Global_Location_Number",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Location_Identifier",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": null
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Default_Ship-To_Location_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Associate_Deliver-To_Location_Reference",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "ID",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Instructional_Site_Data",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Academic_Unit_Reference",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": [{
                "nullable": null,
                "localPart": null,
                "prefix": null,
                "precision": null,
                "name": "ID",
                "namespace": null,
                "scale": null,
                "unicode": null,
                "type": null,
                "maxLength": null,
                "srid": null,
                "subEntities": null
            }]
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Instructional_Site_Data_Capacity",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Off-Site",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Instructional_Use_Only",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Student_Housing_Assignment_Data",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Academic_Unit_Reference",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": [{
                "nullable": null,
                "localPart": null,
                "prefix": null,
                "precision": null,
                "name": "ID",
                "namespace": null,
                "scale": null,
                "unicode": null,
                "type": null,
                "maxLength": null,
                "srid": null,
                "subEntities": null
            }]
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Location_Training_Room_Data",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Link",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Capacity",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Directions",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Equipment",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Reservation_Details",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Video_Conference_Details",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }, {
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Additional_Details",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": null
        }]
    }, {
        "nullable": null,
        "localPart": null,
        "prefix": null,
        "precision": null,
        "name": "Contact_Data",
        "namespace": null,
        "scale": null,
        "unicode": null,
        "type": null,
        "maxLength": null,
        "srid": null,
        "subEntities": [{
            "nullable": null,
            "localPart": null,
            "prefix": null,
            "precision": null,
            "name": "Address_Data",
            "namespace": null,
            "scale": null,
            "unicode": null,
            "type": null,
            "maxLength": null,
            "srid": null,
            "subEntities": [{
                "nullable": null,
                "localPart": null,
                "prefix": null,
                "precision": null,
                "name": "Country_Reference",
                "namespace": null,
                "scale": null,
                "unicode": null,
                "type": null,
                "maxLength": null,
                "srid": null,
                "subEntities": [{
                    "nullable": null,
                    "localPart": null,
                    "prefix": null,
                    "precision": null,
                    "name": "ID",
                    "namespace": null,
                    "scale": null,
                    "unicode": null,
                    "type": null,
                    "maxLength": null,
                    "srid": null,
                    "subEntities": null
                }]
            }, .........

在上面的JSONObject中,我想将$ nullable键替换为$ nullable,将localPart替换为$ localPart等。任何人都可以通过Java代码帮助我。

2 个答案:

答案 0 :(得分:0)

我用以下代码解决了这个问题:

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ReplaceKeyInNestedJson {
public static void removeJSONField(JSONObject obj) throws JSONException {
    obj.put("$nullable", obj.remove("nullable"));
    obj.put("$localPart", obj.remove("localPart"));
    obj.put("$prefix", obj.remove("prefix"));
    obj.put("$precision", obj.remove("precision"));
    obj.put("$name", obj.remove("name"));
    obj.put("$namespace", obj.remove("namespace"));
    obj.put("$scale", obj.remove("scale"));

    Iterator<String> it = obj.keys();
    while (it.hasNext()) {
        String key = it.next();
        Object childObj = obj.get(key);
        if (childObj instanceof JSONArray) {
            JSONArray arrayChildObjs = ((JSONArray) childObj);
            int size = arrayChildObjs.length();
            for (int i = 0; i < size; i++) {
                removeJSONField(arrayChildObjs.getJSONObject(i));
            }
        }
        if (childObj instanceof JSONObject) {
            removeJSONField(((JSONObject) childObj));
        }
    }
}

public static void main(String[] args) {

    String jsonStr = "{\n" + "  \"nullable\": null,\n" + "  \"localPart\": null,\n" + " \"prefix\": null,\n"
            + " \"precision\": null,\n" + " \"name\": \"Put_Location_Request\",\n" + "  \"namespace\": null,\n"
            + " \"scale\": null,\n" + " \"unicode\": null,\n" + "   \"type\": null,\n" + "  \"maxLength\": null,\n"
            + " \"srid\": null,\n" + "  \"subEntities\": [\n" + "     {\n" + "      \"nullable\": null,\n"
            + "     \"localPart\": null,\n" + "     \"prefix\": null,\n" + "        \"precision\": null,\n"
            + "     \"name\": \"Location_Reference\",\n" + "        \"namespace\": null,\n"
            + "     \"scale\": null,\n" + "     \"unicode\": null,\n" + "       \"type\": null,\n"
            + "     \"maxLength\": null,\n" + "     \"srid\": null,\n" + "      \"subEntities\": [{\n"
            + "         \"nullable\": null,\n" + "          \"localPart\": null,\n" + "         \"prefix\": null,\n"
            + "         \"precision\": null,\n" + "         \"name\": \"ID\",\n"
            + "         \"namespace\": null,\n" + "         \"scale\": null,\n" + "         \"unicode\": null,\n"
            + "         \"type\": null,\n" + "          \"maxLength\": null,\n" + "         \"srid\": null,\n"
            + "         \"subEntities\": null\n" + "        }]\n" + "   }, {\n" + "     \"nullable\": null,\n"
            + "     \"localPart\": null,\n" + "     \"prefix\": null,.............";
    try {
        JSONObject obj = new JSONObject(jsonStr);
        removeJSONField(obj);
        System.out.println(obj);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}}

答案 1 :(得分:0)

如果使用速度更快。杰克逊图书馆,您可以通过以下方式解决问题:

import java.io.IOException;
import java.util.Iterator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class ReplaceKeyInNestedJson2 {
private static final String FIELD_TYPE = "FieldType";
public static JsonNode removeJSONField(JsonNode obj) {
    ObjectNode object = (ObjectNode) obj;
    object.put("$kind", FIELD_TYPE);
    object.set("$nullable", object.get("nullable"));
    object.remove("nullable");

    object.set("$localPart", object.get("localPart"));
    object.remove("localPart");

    object.set("$prefix", object.get("prefix"));
    object.remove("prefix");

    object.set("$precision", object.get("precision"));
    object.remove("precision");

    object.set("$name", object.get("name"));
    object.remove("name");

    object.set("$namespace", object.get("namespace"));
    object.remove("namespace");

    object.set("$scale", object.get("scale"));
    object.remove("scale");

    Iterator<String> it = obj.fieldNames();
    while (it.hasNext()) {
        String key = it.next();

        JsonNode childObj = obj.get(key);
        if (childObj.isArray()) {
            ArrayNode arrayChildObjs = ((ArrayNode) childObj);
            int size = arrayChildObjs.size();
            for (int i = 0; i < size; i++) {
                removeJSONField(arrayChildObjs.get(i));
            }
        }
        if (childObj.isObject()) {
            removeJSONField(((JsonNode) childObj));
        }
    }
    return obj;
}

public static void main(String[] args) throws IOException {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode obj = mapper.readTree(StaticData.fieldsJson);
        JsonNode result = removeJSONField(obj);
        System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}