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 ...
随机推荐
- [golang]在Go中处理时区
许多新手开发人员在处理时区时感到困惑. 如何将它们存储在数据库中 如何在Go中解析它们 当将时区存储在数据库中时,请始终遵循一个标准时区,理想的做法是保存UTC时间,并在显示时区时根据需要将其转化为各 ...
- finnal关键字修饰
1.修饰变量,被赋值后不能被赋其他值,相当于常量 2.修饰方法,该方法不可以被子类重写,但可以重载 3.修饰类,修饰的类不可以被继承,如String,Math等
- [内网渗透]Mimikatz使用大全
0x00 简介 Mimikatz 是一款功能强大的轻量级调试神器,通过它你可以提升进程权限注入进程读取进程内存,当然他最大的亮点就是他可以直接从 lsass.exe 进程中获取当前登录系统用户名的密码 ...
- Mono Features of cheat engine
If you attach to a process/game that uses mono, you should see a new "Mono" menu item on t ...
- 《Linux设备驱动程序》第三版 scull编译 Ubuntu18.04
0 准备工作. 0.0 系统环境:Ubuntu18.04.1 amd64. 0.1 安装必要软件包 1 sudo apt install build-essential bison flex libs ...
- ASP.NET_微信JS_SDK调用
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...
- Social GAN: Socially Acceptable Trajectories with Generative Adversarial Networks
Social GAN: Socially Acceptable Trajectories with Generative Adversarial Networks 2019-06-01 09:52:4 ...
- Class as decorator in python
Class as decorator in python . https://www.geeksforgeeks.org/class-as-decorator-in-python/ http://co ...
- Microservices in action: java(spring) and .net
Manning | Homehttps://www.manning.com/ What is a Microservice? | Manninghttps://freecontent.manning. ...
- [转]【Windows小技巧】批量重命名文件
注:如果文件名包含空格,命令应写成ren "s0 (1).gif" s001.gif,简而言之,就是加上双引号!!!原因:系统将s0和(1).gif认为是两个参数,再加上后面的s0 ...