1.  

读取json的key:

  1. /**
  2. * @Description: 递归读取所有的key
  3. * @Param:
  4. * @return:
  5. * @throws Exception
  6. * @author: hw
  7. * @date: 2019/7/31 15:18
  8. */
  9. public static Set<String> getAllKey(JSONObject jsonObject) {
  10. Set<String> myset = new HashSet<>();
  11. Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
  12. while (keys.hasNext()) {
  13. String key = keys.next();
  14. myset.add(key.toString());
  15. if (jsonObject.get(key) instanceof JSONObject) {
  16. JSONObject innerObject = (JSONObject) jsonObject.get(key);
  17. myset.addAll(getAllKey(innerObject));
  18. } else if (jsonObject.get(key) instanceof JSONArray) {
  19. JSONArray innerObject = (JSONArray) jsonObject.get(key);
  20. myset.addAll(getAllKey(innerObject));
  21. }
  22. }
  23. return myset;
  24. }
  25.  
  26. public static Set<String> getAllKey(JSONArray json1) {
  27. Set<String> myset = new HashSet<>();
  28. if (json1 != null ) {
  29. Iterator i1 = json1.iterator();
  30. while (i1.hasNext()) {
  31. Object key = i1.next();
  32. if (key instanceof JSONObject) {
  33. JSONObject innerObject = (JSONObject) key;
  34. myset.addAll(getAllKey(innerObject));
  35. } else if (key instanceof JSONArray) {
  36. JSONArray innerObject = (JSONArray) key;
  37. myset.addAll(getAllKey(innerObject));
  38. }
  39. }
  40. }
  41. return myset;
  42. }

  

替换json的key:

  1. /**
  2. * @Description: 替换json数据的key
  3. * @Param:
  4. * @return:
  5. * @throws Exception
  6. * @author: hw
  7. * @date: 2019/7/31 16:22
  8. */
  9. public static JSONObject changeJsonObj(JSONObject jsonObj,Map<String, String> keyMap) {
  10. JSONObject resJson = new JSONObject();
  11. Set<String> keySet = jsonObj.keySet();
  12. for (String key : keySet) {
  13. String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
  14. try {
  15. if (jsonObj.get(key) instanceof JSONObject) {
  16. JSONObject jsonobj1 = (JSONObject) jsonObj.get(key);
  17. resJson.put(resKey, changeJsonObj(jsonobj1, keyMap));
  18. } else if (jsonObj.get(key) instanceof JSONArray) {
  19. JSONArray jsonArr = (JSONArray) jsonObj.get(key);
  20. resJson.put(resKey, changeJsonArr(jsonArr, keyMap));
  21. }else {
  22. resJson.put(resKey, jsonObj.get(key));
  23. }
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. return resJson;
  29. }
  30.  
  31. public static JSONArray changeJsonArr(JSONArray jsonArr,Map<String, String> keyMap) {
  32. JSONArray resJson = new JSONArray();
  33. for (int i = 0; i < jsonArr.size(); i++) {
  34. JSONObject jsonObj = jsonArr.getJSONObject(i);
  35. resJson.add(changeJsonObj(jsonObj, keyMap));
  36. }
  37. return resJson;
  38. }

  

测试方法:

  1. public static void main(String[] args) {
  2. String test = "{\n" +
  3. "\t\"ret\": \"0\",\n" +
  4. "\t\"retMsg\": null,\n" +
  5. "\t\"count\": 1,\n" +
  6. "\t\"processCost\": null,\n" +
  7. "\t\"data\": [{\n" +
  8. "\t\t\t\"moniid\": \"11\",\n" +
  9. "\t\t\t\"serialnumber\": \"12\",\n" +
  10. "\t\t\t\"monitype\": \"13\"\n" +
  11. "\t\t},\n" +
  12. "\t\t{\n" +
  13. "\t\t\t\"moniid\": \"22\",\n" +
  14. "\t\t\t\"serialnumber\": \"22\",\n" +
  15. "\t\t\t\"monitype\": \"23\"\n" +
  16. "\t\t},\n" +
  17. "\t\t{\n" +
  18. "\t\t\t\"moniid\": \"33\",\n" +
  19. "\t\t\t\"serialnumber\": \"32\",\n" +
  20. "\t\t\t\"monitype\": \"33\"\n" +
  21. "\t\t}\n" +
  22. "\t]\n" +
  23. "}";
  24.  
  25. Map<String, String> keyMap = new HashMap<String, String>();
  26. keyMap.put("count", "param1");
  27. keyMap.put("processCost", "param2");
  28. keyMap.put("desc", "param3");
  29. keyMap.put("moniid", "param4");
  30. keyMap.put("serialnumber", "param5");
  31. keyMap.put("monitype", "param6");
  32. keyMap.put("data", "param7");
  33. JSONObject jsonObj = changeJsonObj(JSONObject.parseObject(test),keyMap);
  34. System.out.println(jsonObj.toString());
  35. }

  

json数据的key的读取和替换的更多相关文章

  1. Java之Hashmap中value为null,则返回json数据中key不存在

    前两天干活儿的时候,将实例对象放在Hashmap中返回给前端: ArtificialEntity artificialEntity = artificialService.getInfoById(id ...

  2. 用js方式取得接口里面json数据的key和value值

    大家在实际操作中难免遇到对接口的问题,想必对一些小白来说取得里面想要是数据也是很是头疼,那么接下来我会结合接口实际情况教大家怎么取得里面相应的数据 接口数据例如:(数据为 模拟数据,json格式) { ...

  3. 保存json数据到本地和读取本地json数据

    private void saveJson(JsonBean bean) { File file = new File(getFilesDir(), "json.txt"); Bu ...

  4. AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取

    一.使用AsyncHttpClient来完成网页源代码的显示功能: 首先.我们引入 步骤: 1.添加网络权限 2.判断网页地址是否为空 3.不为空的情况下创建客户端对象 4.处理get/post请求 ...

  5. 实现JSON数据的存储和读取

    事前准备: //创建一个Crime类 public class Crime { private String mTitle; private UUID mUUID; private Date mDat ...

  6. 修改json数据中key(键值)

    //方法一:修改JSONObject的键 public static JSONObject changeJsonObj(JSONObject jsonObj,Map<String, String ...

  7. json数据的存储与读取

    1.  json数据格式: data = [ {"key1":"xxx","item":"ddd"}, {"k ...

  8. javaScript 对json数据按key值排序

    var ajson= { "result":[ { "cid":1, "name":"aaa", "price ...

  9. localStorage 如何存储JSON数据并读取JSON数据

    localStorage是HTML5提供的再客户端实现本地存储的一种方法,但是localStorage方法只能存储字符串数据,有时候我们需要存储对象到本地比如:JSON:那么,localStorage ...

随机推荐

  1. Python socket 通信功能简介

    常用的地址家族AF_UNIX:基于文件,实现同一主机不同进程之间的通信AF_INET:基于网络,适用于IPv4AF_INET6:基于网络,使用于IPv6 常见的连接类型SOCK_STREAM:即TCP ...

  2. project.config.json在设置了编译模式的时候会出现配置,怎么解决

    因为之前为了方便就选了一个页面进行编译,但是想想回到index首页,就编译了一个pages/index/index. 出现了上面这个,当我再选择编译的时候,还是不会变成之前的. 解决方法是 把红框那段 ...

  3. JS中的constructor 和 prototype

    object.constructor :对象的constructor 属性引用了该对象的构造函数. //例如,用Array()构造函数创建了一个数组,那么a.constructor 引用的就是Arra ...

  4. ssm 项目记录用户操作日志和异常日志

    借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...

  5. docker gitlab and gitlab api

    https://docs.gitlab.com/ee/api/repositories.html curl --header "PRIVATE-TOKEN: fxhDXPRJAowCouXE ...

  6. MYSQL 神奇的操作insert into test select * from test;

    将16行数据复制一份插入数据库,变成32行

  7. Learning Conditioned Graph Structures for Interpretable Visual Question Answering

    Learning Conditioned Graph Structures for Interpretable Visual Question Answering 2019-05-29 00:29:4 ...

  8. 网关 apache APISIX

    网关 apache - 国内版 Binghttps://cn.bing.com/search?q=%E7%BD%91%E5%85%B3+apache&qs=n&form=QBRE&am ...

  9. Hibernate Persistence Contexts

    Hibernate ORM 5.2.18.Final User Guidehttps://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/ ...

  10. uefi下如何启动linux?

    1. 有两种方式 1.1 直接从uefi shell启动linux内核 1.2 从uefi shell启动grub,然后再从grub启动linux内核 2. 需要哪些东西? 2.1 linux内核 2 ...