//方法一:修改JSONObject的键

public static JSONObject changeJsonObj(JSONObject jsonObj,Map<String, String> keyMap) {
   JSONObject resJson = new JSONObject();
Set<String> keySet = jsonObj.keySet();
for (String key : keySet) {
String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
resJson.put(resKey,jsonObj.get(key));
try {
if (jsonObj.get(key) instanceof JSONObject) {
JSONObject jsonobj1 = (JSONObject) jsonObj.get(key);
resJson.put(resKey, changeJsonObj(jsonobj1, keyMap));
} else if (jsonObj.get(key) instanceof JSONArray) {
JSONArray jsonArr = (JSONArray) jsonObj.get(key);
resJson.put(resKey, changeJsonArr(jsonArr, keyMap));
} else if(jsonObj.get(key) instanceof ResponseHelper){  
       //封装的实体类
       //将实体类转换为json格式,修改实体类的键
Object obj = JSONArray.toJSON((ResponseHelper) jsonObj.get(key));
resJson.put(resKey, changeJsonObj(JSONObject.parseObject(obj.toString()), keyMap));
} else {
resJson.put(resKey, jsonObj.get(key));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return resJson;
} //方法二:修改JSONArray的键
public static JSONArray changeJsonArr(JSONArray jsonArr,Map<String, String> keyMap) {
JSONArray resJson = new JSONArray();
for (int i = 0; i < jsonArr.size(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
resJson.add(changeJsonObj(jsonObj, keyMap));
}
return resJson;
} //测试方法:
//测试数据格式大致为: {"info":{"data":[{"lng":"333","lat":"3"}],"count":1}}
//备注:如果实体类中数据latitude为null,则转换时,控制台打印不出来转换后的数据
public static void main(String[] args) {
String json = "{\"areaid\":\""+1+"\"}"; Map<String,String> map = new HashMap<>();
map.put("latitude","lat");
map.put("longitude","lng"); List list = new ArrayList();
Map<String,String> maplist2 = new HashMap<>();
maplist2.put("latitude","3");
maplist2.put("longitude","333");
list.add(maplist2);
ResponseHelper helper = new ResponseHelper(list,list.size());
JSONObject sgxcjson = new JSONObject();
sgxcjson.put("info", helper); JSONObject jsonObject = SfApplicationTests.changeJsonObj(sgxcjson,map);
System.out.println(jsonObject);
}

修改json数据中key(键值)的更多相关文章

  1. 去除json数据的某些键值对

    假如现在要处理的原始数据是字符串.形式如下: var vJson = { name: "张三", class: "软件工程一班" ,other:"无效 ...

  2. Java之Hashmap中value为null,则返回json数据中key不存在

    前两天干活儿的时候,将实例对象放在Hashmap中返回给前端: ArtificialEntity artificialEntity = artificialService.getInfoById(id ...

  3. C#修改json文件中的某些值

    using Newtonsoft.Json; JsonSerializer serialiser = new JsonSerializer(); string newContent = string. ...

  4. 万能正则解析 json 数据 解析成键值对

    string txt = "{\"ip\": \"127.0.0.1\", \"port\": 80, \"status ...

  5. requests(二): json请求中固定键名顺序&消除键和值之间的空格

    继上一篇requests发送json请求的文章后,实际工作中遇到了以下2种情况. 1:服务端要求json字符串,键名的顺序固定  2.服务端对于接收到的json数据中,若key和value之间有空格, ...

  6. C#中当服务器返回的数据json中key的值为数字类型,解决方案

    客户端向服务器发送请求后,服务器返回了一个json格式的字符串但是格式中key的值有些事数字{"1000":"55555"}; 类似这种格式的话就不能直接转化成 ...

  7. 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题

    1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...

  8. json字符串中key值下划线命名转换为驼峰命名

    json字符串中key值下划线命名转换为驼峰命名: 例如: 原json串: String json= "{'user_name':'ok','user_sex':0,'object_info ...

  9. js 替换json对象中的键名

      js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...

随机推荐

  1. rabbitmq安装(ubuntu)

    本文大量复制自:https://blog.csdn.net/qq_22638399/article/details/81704372 # 安装erlang apt-get install erlang ...

  2. JS淘宝浏览商品

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  3. JS 重载父页面

    <script language=javascript> window.onload=function(){ //刷新父页面 window.opener.location.reload() ...

  4. spark集群

    https://blog.csdn.net/boling_cavalry/article/details/86747258 https://www.cnblogs.com/xuliangxing/p/ ...

  5. vsto 学习

    Object到String类型转换的四种方式 通常object到string有四种方式:(假设有object obj) obj.ToString, Convert.ToString, (string) ...

  6. 让我们纯手写一个js继承吧

    继承在前端逻辑操作中是比较常见的,今天我们就从零开始写一个js的继承方式 在es5中继承实质上是先创建子类的实例对象,然后再将父类的方法添加到this上Parent.call(this),在es6中则 ...

  7. 论文-MobileNet-V1、ShuffleNet-V1、MobileNet-V2、ShuffleNet-V2、MobileNet-V3

    1.结构对比 1)MobileNet-V1 2)ShuffleNet-V1 3)MobileNet-V2 4)ShuffleNet-V2

  8. 在VS的依赖项中引用项目

    操作步骤:鼠标右击项目(注意是项目)->添加->引用->项目(在项目列表中选择需要引用的项目)->确定

  9. Selenium-浏览器兼容性测试自动化

    Selenium 使浏览器兼容性测试自动化成为可能,但是在不同的浏览器上依然有细微的差别 测试浏览器的兼容性--测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上. 测试系统功能--创建回 ...

  10. ng-模板语法

    插值 文本绑定 <p>Message: {{ msg }}</p> <p [innerHTML]="msg"></p> 属性绑定 & ...