当前位置:   article > 正文

gson使用 json

gson使用 json
  1. Gson gson=new Gson();
  2. String jj="{\"connect\":[{\"name\":\"11111_xxx_22222\",\"source\":\"11111_o_1\",\"target\":\"22222_i_0\"},{\"name\":\"11111_xxbx_22222\",\"source\":\"11111_o_0\",\"target\":\"22222_i_1\"}],\"nodes\":[{\"id\":\"11111111111111111111\",\"i\":1,\"o\":2,\"x\":100,\"y\":100,\"text\":\" SQL转换器1----\",\"compid\":\"89789451897489456189465413153\",\"nodeid\":\"0060011c3545419c80c2e2b32a5b25da\",\"comptype\":\"SQLconverter\",\"compname\":\"SQL转换器\",\"pic\":\"gl.png\",\"title\":\"节点名称: SQL转换器1----\n算法名称:SQL转换器\",\"state\":\"\",\"params\":{\"x_cols\":[\"LIMIT_BAL\",\"SEX\",\"EDUCATION\",\"MARRIAGE\",\"AGE\",\"PAY_0\",\"PAY_2\",\"PAY_3\",\"PAY_4\",\"PAY_5\",\"PAY_6\",\"BILL_AMT1\",\"BILL_AMT2\",\"BILL_AMT3\",\"BILL_AMT4\",\"BILL_AMT5\",\"BILL_AMT6\",\"PAY_AMT1\",\"PAY_AMT2\",\"PAY_AMT3\",\"PAY_AMT4\",\"PAY_AMT5\",\"PAY_AMT6\"],\"y_col\":\"next_month\",\"param\":\"{\\\"fitIntercept\\\":\\\"true\\\",\\\"tol\\\":\\\"1.000001\\\",\\\"regParam\\\":\\\"0.1\\\",\\\"maxIter\\\":\\\"1004\\\",\\\"elasticNetParam\\\":\\\"0.4\\\"}\"}},{\"nodeid\":\"22222\",\"i\":2,\"o\":1,\"x\":500,\"y\":600},{\"nodeid\":\"33333\",\"i\":1,\"o\":1,\"x\":500,\"y\":170}],\"scale\":1,\"translateX\":0,\"translateY\":0}";
  3. Root root = gson.fromJson(jj, Root.class);
  4. System.out.println(root.getNodes().get(0).getText());
  5. String param=root.getNodes().get(0).getParams().getParam();
  6. System.out.println(param);
  7. List<String> x_cols=root.getNodes().get(0).getParams().getX_cols();
  8. for (int i = 0; i < x_cols.size(); i++) {
  9. //System.out.println(i+":::"+x_cols.get(i));
  10. }
  11. Type type = new TypeToken<Map<String, String>>() {}.getType();
  12. Map<String, String> map = gson.fromJson(param, type);
  13. String showString = "";
  14. for (String keyString : map.keySet()) {
  15. showString += keyString + ":" + map.get(keyString) + "\n-----\n";
  16. }
  17. System.out.println(showString);

创建对应实体类

可以自动生成:

https://www.sojson.com/json2entity.html


转map接收二级参数{"fitIntercept":"true","tol":"1.000001","regParam":"0.1","maxIter":"1004","elasticNetParam":"0.4"}

原json文件

  1. {
  2. "connect": [
  3. {
  4. "name": "11111_xxx_22222",
  5. "source": "11111_o_1",
  6. "target": "22222_i_0"
  7. },
  8. {
  9. "name": "11111_xxbx_22222",
  10. "source": "11111_o_0",
  11. "target": "22222_i_1"
  12. }
  13. ],
  14. "nodes": [
  15. {
  16. "id": "11111111111111111111",
  17. "i": 1,
  18. "o": 2,
  19. "x": 100,
  20. "y": 100,
  21. "text": " SQL转换器1----",
  22. "compid": "89789451897489456189465413153",
  23. "nodeid": "0060011c3545419c80c2e2b32a5b25da",
  24. "comptype": "SQLconverter",
  25. "compname": "SQL转换器",
  26. "pic": "gl.png",
  27. "title": "节点名称: SQL转换器1----\n算法名称:SQL转换器",
  28. "state": "",
  29. "params": {
  30. "x_cols": [
  31. "LIMIT_BAL",
  32. "SEX",
  33. "EDUCATION",
  34. "MARRIAGE",
  35. "AGE",
  36. "PAY_0",
  37. "PAY_2",
  38. "PAY_3",
  39. "PAY_4",
  40. "PAY_5",
  41. "PAY_6",
  42. "BILL_AMT1",
  43. "BILL_AMT2",
  44. "BILL_AMT3",
  45. "BILL_AMT4",
  46. "BILL_AMT5",
  47. "BILL_AMT6",
  48. "PAY_AMT1",
  49. "PAY_AMT2",
  50. "PAY_AMT3",
  51. "PAY_AMT4",
  52. "PAY_AMT5",
  53. "PAY_AMT6"
  54. ],
  55. "y_col": "next_month",
  56. "param": "{\"fitIntercept\":\"true\",\"tol\":\"1.000001\",\"regParam\":\"0.1\",\"maxIter\":\"1004\",\"elasticNetParam\":\"0.4\"}"
  57. }
  58. },
  59. {
  60. "nodeid": "22222",
  61. "i": 2,
  62. "o": 1,
  63. "x": 500,
  64. "y": 600
  65. },
  66. {
  67. "nodeid": "33333",
  68. "i": 1,
  69. "o": 1,
  70. "x": 500,
  71. "y": 170
  72. }
  73. ],
  74. "scale": 1,
  75. "translateX": 0,
  76. "translateY": 0
  77. }
参考地址 https://blog.csdn.net/axuanqq/article/details/51441590
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/71850
推荐阅读
相关标签
  

闽ICP备14008679号