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 ...
随机推荐
- 32、reduceByKey和groupByKey对比
一.groupByKey 1.图解 val counts = pairs.groupByKey().map(wordCounts => (wordCounts._1, wordCounts._2 ...
- Neither shaken nor stirred(DFS理解+vector存图)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2013 题目理解: 给定n个点的有向图: 下面n行,第一个数字表示点权,后面一个数字m表示 ...
- 2019强网杯web upload writeup及关键思路
<?phpnamespace app\web\controller; class Profile{ public $checker; public $filename_tmp; ...
- Python 的特性
Copyright © 1999-2019, CSDN.NET, All Rights Reserved 原 python面试题整理(一) 崔先生的博客阅读数:2402018-08-03 前言 ...
- python提取计算结果的最大最小值及其坐标
我们在fluent当中后处理的时候,可以通过fluent本身得到某些物理量的最大值和最小值,但是我们却无法确定这些最大值和最小值的具体位置.其实我们可以将求解数据导出以后,借助python求得最大值和 ...
- OpenFOAM在原有算例上新建算例(只拷贝0,system,constant)
原视频下载地址: https://yunpan.cn/cMpyBHSEvC7T4 (提取码:dca4)
- arts打卡13周
算法: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 12. 113. 214. 12115. 1112211 被读作 "one 1" ...
- 安卓开发实战之app之版本更新升级(DownloadManager和http下载)完整实现
转载: https://blog.csdn.net/u013278099/article/details/52692008 前言 本文将讲解app的升级与更新.一般而言用户使用App的时候升级提醒有两 ...
- webpack4-用之初体验
众所周知,webpack进入第4个大版本已经有2个月的时间了,而且webpack团队升级更新的速度也是非常的惊人 在写下如下内容的时候webpack已经出到了4.6的版本了,剑指5.0应该是指日可待了 ...
- Android 9.0 Http不能访问网络
最近在做公司产品,一期完成,打包给测试,然后....一台手机连服务器都访问不了看日志如下: UnityWebRequest返回code:0,显示Unknow error 服务器接口是http://非域 ...