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 ...
随机推荐
- Python socket 通信功能简介
常用的地址家族AF_UNIX:基于文件,实现同一主机不同进程之间的通信AF_INET:基于网络,适用于IPv4AF_INET6:基于网络,使用于IPv6 常见的连接类型SOCK_STREAM:即TCP ...
- project.config.json在设置了编译模式的时候会出现配置,怎么解决
因为之前为了方便就选了一个页面进行编译,但是想想回到index首页,就编译了一个pages/index/index. 出现了上面这个,当我再选择编译的时候,还是不会变成之前的. 解决方法是 把红框那段 ...
- JS中的constructor 和 prototype
object.constructor :对象的constructor 属性引用了该对象的构造函数. //例如,用Array()构造函数创建了一个数组,那么a.constructor 引用的就是Arra ...
- ssm 项目记录用户操作日志和异常日志
借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...
- docker gitlab and gitlab api
https://docs.gitlab.com/ee/api/repositories.html curl --header "PRIVATE-TOKEN: fxhDXPRJAowCouXE ...
- MYSQL 神奇的操作insert into test select * from test;
将16行数据复制一份插入数据库,变成32行
- Learning Conditioned Graph Structures for Interpretable Visual Question Answering
Learning Conditioned Graph Structures for Interpretable Visual Question Answering 2019-05-29 00:29:4 ...
- 网关 apache APISIX
网关 apache - 国内版 Binghttps://cn.bing.com/search?q=%E7%BD%91%E5%85%B3+apache&qs=n&form=QBRE&am ...
- Hibernate Persistence Contexts
Hibernate ORM 5.2.18.Final User Guidehttps://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/ ...
- uefi下如何启动linux?
1. 有两种方式 1.1 直接从uefi shell启动linux内核 1.2 从uefi shell启动grub,然后再从grub启动linux内核 2. 需要哪些东西? 2.1 linux内核 2 ...