JSON中所有key首字母变小写(JAVA实现)

AJavaclass,JsonUtils,demonstratesconvertingallkeysinaJSONobjecttolowercaseusingGsonlibrary.
摘要由CSDN通过智能技术生成
package com.taxbureau.tax.util;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class JsonUtils {

     /**
	     * 对JSONObject的所有首字母key转为小写
	 *
	 * @param jsonString
	 * @return 
	 */
    public static JsonObject convertKeysToLowerCase(String jsonString) {
        JsonElement jsonElement = JsonParser.parseString(jsonString);
        return convertKeysToLowerCase(jsonElement.getAsJsonObject());
    }

    private static JsonObject convertKeysToLowerCase(JsonObject jsonObject) {
        JsonObject newJsonObject = new JsonObject();
        for (String key : jsonObject.keySet()) {
            String newKey = key.substring(0, 1).toLowerCase() + key.substring(1);
            JsonElement value = jsonObject.get(key);
            if (value.isJsonObject()) {
                value = convertKeysToLowerCase(value.getAsJsonObject());
                newJsonObject.add(newKey, value);
            }else if(value.isJsonArray()) {
            	JsonArray jsonArray = value.getAsJsonArray();
            	JsonArray newArray = new JsonArray();
            	for(int i=0;i<jsonArray.size();i++) {
            		value = convertKeysToLowerCase(jsonArray.get(i).getAsJsonObject());
            		newArray.add(value);
            	}
            	 newJsonObject.add(newKey, newArray);
            }else {
            	newJsonObject.add(newKey, value);
            }
            
        }
        return newJsonObject;
    }
}


测试代码

public static void main(String[] args) {
		
		String s = "";
		com.google.gson.JsonObject res = null;
		try {
			res = JsonUtils.convertKeysToLowerCase(s);
			System.out.println(res);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要根据key字母排序JSON,可以使用JavaJSONObject和TreeMap来实现。首先,将JSON字符串解析为JSONObject对象。然后,创建一个TreeMap对象,并通过迭代JSONObject的keySet获取所有的key。将这些key添加到TreeMap,由于TreeMap会根据其自然排序对元素进行排序,所以这些key会按照首字母进行排序。最后,通过遍历TreeMap的entrySet,可以获取到按首字母排序的key和对应的value,可以将其重新组装成一个新的JSON对象。 下面是具体的代码实现: ```java import org.json.JSONObject; import java.util.Iterator; import java.util.TreeMap; public class Main { public static void main(String[] args) { String jsonStr = "{\"name\": \"John\", \"age\": 30, \"address\": \"New York\"}"; // 解析JSON字符串为JSONObject对象 JSONObject jsonObject = new JSONObject(jsonStr); // 创建TreeMap并将JSONObject的key按首字母排序 TreeMap<String, Object> sortedMap = new TreeMap<>(); Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); sortedMap.put(key, jsonObject.get(key)); } // 构建新的JSONObject对象 JSONObject sortedJson = new JSONObject(); for (String key : sortedMap.keySet()) { sortedJson.put(key, sortedMap.get(key)); } System.out.println(sortedJson.toString()); } } ``` 以上代码会输出按首字母排序后的JSON字符串: ```json {"address":"New York","age":30,"name":"John"} ``` 这样就实现了根据key字母排序JSON的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liberty888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值