How to convert json object into map key-value pair in Java

Tool method: The purpose of this article is to convert the json string into a map key-value pair storage, and only store the data of the leaf nodes

maven reference jar package version:

  1. <dependency>
  2. <groupId>org.json</groupId>
  3. <artifactId>json</artifactId>
  4. <version>20090211</version>
  5. </dependency>
copy code

Tools:

  1. package com.baofoo.admin.test;
  2. //import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.junit.Test;
  5. import org.json.*; Jiangsu Surrogacy
  6. import java.util.*;   Jiangsu Surrogacy Company
  7. /**
  8. * Created by BF100 on 2018/4/12.
  9. */
  10. @ Slf4j
  11. public class TestByCaoxNew {
  12. [url=home.php?mod=space&uid=5447]@test[/url]
  13. public void test1(){
  14. try{
  15. int a = 1/0;
  16. }catch (Exception e){
  17. log.error("call Exception :{}",e);
  18. e.printStackTrace ();
  19. }
  20. System.out.println("come on !!!");
  21. }
  22. @Test
  23. public void test2() throws Exception{
  24. String str = "{\"result\":\"success\",\"message\":\"成功!\"}";
  25. String str2 = "{result:success,message:成功}";
  26. // JSONObject jsStr = JSONObject.parseObject(str);
  27. // System.out.println(jsStr);
  28. JSONObject obj = new JSONObject(str);
  29. Stack<JSONObject> stObj = new Stack<JSONObject>();
  30. stObj.push(obj);
  31. Map<String, Object> resultMap =new HashMap<String, Object>();
  32. JsonToMap(stObj,resultMap);
  33. Set<String> keys = resultMap.keySet();
  34. for (String key:keys){
  35. System.out.println(key+":"+resultMap.get(key));
  36. }
  37. }
  38. /**
  39. * @Author:sks
  40. * @Description: Store the json object data in the map in the form of key-value pairs, only store leaf nodes
  41. * @Date:
  42. */
  43. private static void JsonToMap(Stack<JSONObject> stObj, Map<String, Object> resultMap) throws Exception {
  44. if(stObj == null && stObj.pop() == null){
  45. return ;
  46. }
  47. JSONObject json = stObj.pop();
  48. Iterator it = json.keys();
  49. while(it.hasNext()){
  50. String key = (String) it.next();
  51. //get the value of value
  52. Object value = json.get(key);
  53. //System.out.println(value);
  54. if(value instanceof JSONObject)
  55. {
  56. stObj.push((JSONObject)value);
  57. // recursively traverse
  58. JsonToMap(stObj,resultMap);
  59. }
  60. else {
  61. resultMap.put(key, value);
  62. }
  63. }
  64. }
  65. @Test
  66. public void test3() throws Exception{
  67. String jsonStr ="{responseHeader:{status:0,QTime:0},spellcheck:{suggestions:{中国:{numFound:9,startOffset:0,endOffset:2," +
  68. "suggestion:[Industrial and Commercial Bank of China, Chinese People, China International, Chinese Agriculture, Chinese Market, Chinese Economy, Chinese, Chinese Broadcasting, Chinese Culture]}}," +
  69. "collations:{collation:ICBC}}}";
  70. JSONObject obj = new JSONObject(jsonStr);
  71. Stack<JSONObject> stObj = new Stack<JSONObject>();
  72. stObj.push(obj);
  73. Map<String, Object> resultMap =new HashMap<String, Object>();
  74. JsonToMap(stObj,resultMap);
  75. Set<String> keys = resultMap.keySet();
  76. for (String key:keys){
  77. System.out.println(key+":"+resultMap.get(key));
  78. }
  79. }
  80. }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325235401&siteId=291194637