JSONObject和JSONArray中的key值转大写或小写的通用方法

一.有时需要json数据的key全部转成大写或者转成小写
二.直接上代码,一封装成一个工具类了,直接传入JSONObject或JSONArray对象,调用数组或对象的方法就能实现.
package cn.wisesign.commonutil.jsonpath;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**

  • 修改JSON的key为小写或者大写
    */
    @SuppressWarnings(“rawtypes”)
    public class JSONKeyTransfer {

    public static final String JSONOBJECT_KEY = “JSONObject”;

    public static final String JSONARRAY_KEY = “JSONArray”;

    /**

    • 修改json的key为大写或者小写
    • @throws
      */
      public static JSONObject transferJsonKey(JSONObject json) {
      JSONObject object = new JSONObject();
      if (json.getClass().toGenericString().endsWith(JSONOBJECT_KEY)) {
      JSONObject jsonObject = (JSONObject)json;
      Iterator iterator = jsonObject.keys();
      while (iterator.hasNext()) {
      String jsonKey = (String) iterator.next();
      Object valueObject = jsonObject.get(jsonKey);
      if (valueObject.getClass().toString().endsWith(JSONOBJECT_KEY)) {
      JSONObject checkObject = JSONObject.fromObject(valueObject);
      // 当值为null时,valueObject还是JSONObject对象,判空是不成立的,要判断是否是nullObject
      if (!checkObject.isNullObject()) {
      object.accumulate(jsonKey.toUpperCase(), transferJsonKey((JSONObject) valueObject));
      } else {
      object.accumulate(jsonKey, null);
      }
      } else if (valueObject.getClass().toString().endsWith(JSONARRAY_KEY)) {
      object.accumulate(jsonKey.toUpperCase(), transferJsonArray(jsonObject.getJSONArray(jsonKey)));
      }else{
      object.accumulate(jsonKey.toUpperCase(), valueObject);
      }
      }
      }
      return object;
      }

    /**

    • JSONArray key大小写转换
    • @throws
      */
      public static JSONArray transferJsonArray(JSONArray jsonArray) {
      JSONArray array = new JSONArray();
      if (null != jsonArray && jsonArray.size() > 0) {
      for (Object object : jsonArray) {
      if (object.getClass().toString().endsWith(JSONOBJECT_KEY)) {
      array.add(transferJsonKey((JSONObject) object));
      } else if (object.getClass().toString().endsWith(JSONARRAY_KEY)) {
      array.add(transferJsonArray((JSONArray) object));
      }
      }
      }
      return array;
      }

    public static void main(String[] args) {
    String jsonStr = “”;
    try {
    File jsonFile = new File(“d:\test.json”);
    FileReader fileReader = new FileReader(jsonFile);

         Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
         int ch = 0;
         StringBuffer sb = new StringBuffer();
         while ((ch = reader.read()) != -1) {
             sb.append((char) ch);
         }
         fileReader.close();
         reader.close();
         jsonStr = sb.toString();
    

// jsonStr = jsonStr.toUpperCase();
}catch(Exception e){

    }
	//String str = "{\"contextDTO\":{\"appPackAccToken\":null,\"cAllChainId\":\"\",\"callCount\":0,\"currentCallId\":\"0\",\"employeeId\":\"00000000-0000-0000-0000-000000000000\",\"id\":null,\"latitude\":null,\"loginCurrentCulture\":0,\"loginDepartment\":\"00000000-0000-0000-0000-000000000000\",\"loginDepartmentName\":null,\"loginIP\":null,\"loginOrg\":\"00000000-0000-0000-0000-000000000000\",\"loginTenantId\":\"00000000-0000-0000-0000-000000000000\",\"loginTenantName\":null,\",\"loginUserCode\":\"dddddd\",\"loginUserID\":\"79694a50-ea5f-4d66-99ef-bc7661cc2477\",\"loginUserName\":\"测试\",\"longitude\":null,\"sessionID\":\"97571283-6eed-4fd5-9d7c-6c60dbd362c9\"},\"statusCode\":null}";
	JSONObject object = JSONObject.fromObject(jsonStr);
    //JSONArray object = JSONArray.fromObject(jsonStr);
	object = transferJsonKey(object);
	//object = transferJsonArray(object);
	System.out.println(object.toString());
}

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值