首页 / JSON / json数据的key的读取和替换
json数据的key的读取和替换
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了json数据的key的读取和替换,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3370字,纯文字阅读大概需要5分钟。
内容图文
![json数据的key的读取和替换](/upload/InfoBanner/zyjiaocheng/1046/aa219167c3154ae99d831f11c2b3e77f.jpg)
读取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()); }
原文:https://www.cnblogs.com/weihuang6620/p/11308832.html
内容总结
以上是互联网集市为您收集整理的json数据的key的读取和替换全部内容,希望文章能够帮你解决json数据的key的读取和替换所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。