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 ...
随机推荐
- vgg16中的函数
1.inspect.getfile(文件名)文件名一般与类名相同,返回文件目录包含文件名 import inspect class os测试: def __init__(self): path = i ...
- SpringContextHolder 作用
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. 使用方式.在启动类里添加Bean package com.y ...
- Java垃圾回收(java GC)
一.GC的阶段 对每个对象而言,垃圾回收分为两个阶段:finalization和reclamation. finalization: 指运行这个对象的finalize的方法. reclamation: ...
- Android反编译,apk反编译技术总结
1.谷歌提供的工具:android-classyshark 下载地址:https://github.com/google/android-classyshark/releases,下载下来之后是一个可 ...
- html5中time元素详解
html5中time元素详解 一.总结 一句话总结: time的使用的话主要是将时间放在datetime属性里面:<time datetime="2015-10-22"> ...
- RTSP、RTMP、HTTP协议
一.异同1.RSTP.RTMP.HTTP协议共同点RTSP RTMP HTTP都是用在应用层.理论上这三种协议都可以做直播和点播,但直播一般用RTSP和RTMP点播用HTTP.2.RSTP.RTMP. ...
- Vuejs函数式组件,你值得拥有(1)
函数式组件在React社区很流行使用,那么在vue里面我们要怎么用呢 下面会涉及到的知识点: 高阶函数.状态.实例.vue组件 什么是函数式组件 我们可以把函数式组件想像成组件里的一个函数,入参是渲染 ...
- 006 DOM节点操作与元素的创建
一:节点 1.节本基本概念 节点主要有标签,属性,文本[包括文字,空格,换行,回车]. 2.节点的属性 可以使用标签,元素点出来 可以使用标签,点出来 可以使用文本,点出来 nodeType:1--标 ...
- EDAS Serverless & Kubernetes SLB LVS Nginx
分布式缓存负载均衡的规则处理:虚拟节点对一致性哈希的改进 - yanghuahui - 博客园https://www.cnblogs.com/yanghuahui/p/3755460.html EDA ...
- 将移远通信的EC20驱动移植到NUC972上(转)
源: 将移远通信的EC20驱动移植到NUC972上