json数据的key的读取和替换
读取json的key:
/**
* @Description: 递归读取所有的key
* @Param:
* @return:
* @throws Exception
* @author: hw
* @date: 2019/7/31 15:18
*/
public static Set<String> getAllKey(JSONObject jsonObject) {
Set<String> myset = new HashSet<>();
Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
myset.add(key.toString());
if (jsonObject.get(key) instanceof JSONObject) {
JSONObject innerObject = (JSONObject) jsonObject.get(key);
myset.addAll(getAllKey(innerObject));
} else if (jsonObject.get(key) instanceof JSONArray) {
JSONArray innerObject = (JSONArray) jsonObject.get(key);
myset.addAll(getAllKey(innerObject));
}
}
return myset;
} public static Set<String> getAllKey(JSONArray json1) {
Set<String> myset = new HashSet<>();
if (json1 != null ) {
Iterator i1 = json1.iterator();
while (i1.hasNext()) {
Object key = i1.next();
if (key instanceof JSONObject) {
JSONObject innerObject = (JSONObject) key;
myset.addAll(getAllKey(innerObject));
} else if (key instanceof JSONArray) {
JSONArray innerObject = (JSONArray) key;
myset.addAll(getAllKey(innerObject));
}
}
}
return myset;
}
替换json的key:
/**
* @Description: 替换json数据的key
* @Param:
* @return:
* @throws Exception
* @author: hw
* @date: 2019/7/31 16:22
*/
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);
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 {
resJson.put(resKey, jsonObj.get(key));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return resJson;
} 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;
}
测试方法:
public static void main(String[] args) {
String test = "{\n" +
"\t\"ret\": \"0\",\n" +
"\t\"retMsg\": null,\n" +
"\t\"count\": 1,\n" +
"\t\"processCost\": null,\n" +
"\t\"data\": [{\n" +
"\t\t\t\"moniid\": \"11\",\n" +
"\t\t\t\"serialnumber\": \"12\",\n" +
"\t\t\t\"monitype\": \"13\"\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\"moniid\": \"22\",\n" +
"\t\t\t\"serialnumber\": \"22\",\n" +
"\t\t\t\"monitype\": \"23\"\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\"moniid\": \"33\",\n" +
"\t\t\t\"serialnumber\": \"32\",\n" +
"\t\t\t\"monitype\": \"33\"\n" +
"\t\t}\n" +
"\t]\n" +
"}"; Map<String, String> keyMap = new HashMap<String, String>();
keyMap.put("count", "param1");
keyMap.put("processCost", "param2");
keyMap.put("desc", "param3");
keyMap.put("moniid", "param4");
keyMap.put("serialnumber", "param5");
keyMap.put("monitype", "param6");
keyMap.put("data", "param7");
JSONObject jsonObj = changeJsonObj(JSONObject.parseObject(test),keyMap);
System.out.println(jsonObj.toString());
}
json数据的key的读取和替换的更多相关文章
- Java之Hashmap中value为null,则返回json数据中key不存在
前两天干活儿的时候,将实例对象放在Hashmap中返回给前端: ArtificialEntity artificialEntity = artificialService.getInfoById(id ...
- 用js方式取得接口里面json数据的key和value值
大家在实际操作中难免遇到对接口的问题,想必对一些小白来说取得里面想要是数据也是很是头疼,那么接下来我会结合接口实际情况教大家怎么取得里面相应的数据 接口数据例如:(数据为 模拟数据,json格式) { ...
- 保存json数据到本地和读取本地json数据
private void saveJson(JsonBean bean) { File file = new File(getFilesDir(), "json.txt"); Bu ...
- AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取
一.使用AsyncHttpClient来完成网页源代码的显示功能: 首先.我们引入 步骤: 1.添加网络权限 2.判断网页地址是否为空 3.不为空的情况下创建客户端对象 4.处理get/post请求 ...
- 实现JSON数据的存储和读取
事前准备: //创建一个Crime类 public class Crime { private String mTitle; private UUID mUUID; private Date mDat ...
- 修改json数据中key(键值)
//方法一:修改JSONObject的键 public static JSONObject changeJsonObj(JSONObject jsonObj,Map<String, String ...
- json数据的存储与读取
1. json数据格式: data = [ {"key1":"xxx","item":"ddd"}, {"k ...
- javaScript 对json数据按key值排序
var ajson= { "result":[ { "cid":1, "name":"aaa", "price ...
- localStorage 如何存储JSON数据并读取JSON数据
localStorage是HTML5提供的再客户端实现本地存储的一种方法,但是localStorage方法只能存储字符串数据,有时候我们需要存储对象到本地比如:JSON:那么,localStorage ...
随机推荐
- AttributeError: module 'tensorflow' has no attribute 'set_random_seed'
anaconda3 python3.7 安装好tensorflow 后出现上面的问题,原因是安装的tensorflow版本是2.0的,所以使用以前的代码tensorflow中的函数不兼容.
- (21)打鸡儿教你Vue.js
组件化思想: 组件化实现功能模块的复用 高执行效率 开发单页面复杂应用 组件状态管理(vuex) 多组件的混合使用 vue-router 代码规范 vue-router <template> ...
- mac系统提示 interactive intelligence 的恼人问题
处理 interacti intelligence 提示问题记录 二手购买了一台电脑,从最初的小白到现在稍微熟悉mac的使用, 一直困扰我的便是一个提示, 上图 困扰多年, 记录一下解决和尝试过程吧. ...
- Windows系统清除占用的串口号列表批处理
蛋疼总是无缘无故被占用 @echo off reg query "HKLM\SYSTEM\CurrentControlSet\Control\COM Name Arbiter" / ...
- SQL数据清洗
大家好,我是jacky,很高兴继续跟大家分享<MySQL数据分析实战>,从本节课程开始,我们的课程就会变得越来越实战,也会越来越有意思了: 我们课程的主体叫MySQL数据分析实战,那我们用 ...
- shell 一次性杀掉相同名称的进程
这条命令将会杀掉所有含有关键字"LOCAL=NO"的进程 ps -ef|grep LOCAL=NO|grep -v grep|cut -c -|xargs kill - 另一种方法 ...
- [NOI.AC]NOI2019省选模拟赛 第二场
传送门 Solution A. 一共有\(T\)组数据 每次询问你\([l,r]\)中有多少个数能被他的所有数位整除(如果数位中含有\(0\)忽略掉) 数位dp,咕咕咕 B. 题面略 考虑一个个只有两 ...
- 小程序弹框wx.showModal的使用
if (!logined) { wx.showModal({ title: '提示', content: '您还没登录登录车掌柜, 是否前往登录', confirmText: '前往登录', conf ...
- 页面上有tab,如何点击加载更多?
加载更多是一个很简单的东西.但是也有几点需要注意: 1.首先在你切换tab的时候,要么在调用这个函数的时候将这个的thispage设为1,要么在切换tab的时候将这个thispage设为1,当你将这个 ...
- 第十四周助教工作总结——NWNU李泓毅
助教博客链接:https://www.cnblogs.com/NWNU-LHY/ 本次作业的要求:团队项目需求改进与系统设计:https://www.cnblogs.com/nwnu-daizh/p/ ...