替换JSON键中的空格 [英] Replace whitespace in JSON keys

查看:136
本文介绍了替换JSON键中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑用下划线替换JSON密钥中所有空格的最佳解决方案.

I am thinking of a best solution to replace all the whitespaces in JSON keys with underscore.

{ 
  "Format": "JSON", 
  "TestData": { 
    "Key with Spaces in it": { 
      "And Again": { 
        "Child_Key1": "Financial", 
        "Child_Key2": null 
      }, 
......... 
..... 

我希望上述内容按如下所示进行转换:

I want the above to be converted as shown below:

{ 
  "Format": "JSON", 
  "TestData": { 
    "Key_with_Spaces_in_it": { 
      "And_Again": { 
        "Child_Key1": "Financial", 
        "Child_Key2": null 
      }, 
......... 
..... 

有什么建议吗?

任何Java库都具有任何预定义的功能来做到这一点吗?

Does any Java library have any predefined function to do this ?

推荐答案

替换密钥

以下代码使用Google的JSON解析器提取密钥,将其重新格式化,然后创建一个新的JSON对象:

The following code uses Google's JSON parser to extract keys, reformat them, and then create a new JSON object:

public static void main(String[] args) {
    String testJSON = "{\"TestKey\": \"TEST\", \"Test spaces\": { \"child spaces 1\": \"child value 1\", \"child spaces 2\": \"child value 2\" } }";
    Map oldJSONObject = new Gson().fromJson(testJSON, Map.class);
    JsonObject newJSONObject = iterateJSON(oldJSONObject);

    Gson someGson = new Gson();
    String outputJson = someGson.toJson(newJSONObject);
    System.out.println(outputJson);
}

private static JsonObject iterateJSON(Map JSONData) {
    JsonObject newJSONObject = new JsonObject();
    Set jsonKeys = JSONData.keySet();
    Iterator<?> keys = jsonKeys.iterator();
    while(keys.hasNext()) {
        String currentKey = (String) keys.next();
        String newKey = currentKey.replaceAll(" ", "_");
        if (JSONData.get(currentKey) instanceof Map) {
            JsonObject currentValue = iterateJSON((Map) JSONData.get(currentKey));
            newJSONObject.add(currentKey, currentValue);
        } else {
            String currentValue = (String) JSONData.get(currentKey);
            newJSONObject.addProperty(newKey, currentValue);
        }
    }
    return newJSONObject;
}

您可以在此处了解更多有关GSON的信息.

You can read more about GSON here.

替换值

取决于如何设置JSON数据,您可能需要使用JSONObject切换JSONArray.

Depending on how your JSON data is set up, you might need to switch JSONArray with JSONObject.

JSONArrays以[]开头和结尾,而JSONObjects以{}

JSONArrays begin and end with [], while JSONObjects begin and end with {}

简而言之,这些方法将遍历整个数组/对象,并用下划线替换任何空格.它们是递归的,因此将深入子JSONArrays/JSONObjects.

In short, these methods will travel over an entire array/object and replace any spaces with underscores. They're recursive, so they will dive into child JSONArrays/JSONObjects.

如果JSON数据编码为Java JSONArray,则可以执行以下操作:

If the JSON data is encoded as a Java JSONArray, you can do the following:

public static void removeJSONSpaces(JSONArray theJSON) {
    for (int i = 0; while i < theJSON.length(); i++) {
        if (theJSON.get(i) instanceof JSONArray) {
            currentJSONArray = theJSON.getJSONArray(i);
            removeJSONSpaces(currentJSONArray);
        } else {
            currentEntry = theJSON.getString(i);
            fixedEntry = currentEntry.replace(" ", "_");
            currentJSONArray.put(i, fixedEntry);
        }
    }
}

简而言之,此方法将遍历整个数组,并用下划线替换任何空格.它是递归的,因此将深入子JSONArrays.

In short, this method will travel over an entire array and replace any spaces with underscores. It's recursive, so it will dive into child JSONArrays.

您可以在此处

如果数据编码为JSONObject,则需要执行以下操作:

If the data is encoded as a JSONObject, you'd want to do something like:

public static void removeJSONSpaces(JSONObject theJSON) {

    jObject = new JSONObject(theJSON.trim());
    Iterator<?> keys = jObject.keys();

    while(keys.hasNext()) {
        String key = (String)keys.next();
        if (jObject.get(key) instanceof JSONObject) {
            removeJSONSpaces(jObject.get(key))
        } else {
            currentEntry = theJSON.getString(i);
            fixedEntry = currentEntry.replace(" ", "_");
            currentJSONArray.put(i, fixedEntry);
        }
    }

}

您可以在此处

这篇关于替换JSON键中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆