两个哈希映射到实体类

huangapple go评论93阅读模式
英文:

Two hashmap to entity class

问题

  1. 我有两个`HashMap`我想使用GSON进行反序列化为实体类
  2. void convert(){
  3. HashMap<String,String> map1 = new HashMap<>();
  4. map1.put("key1","value1");
  5. map1.put("key2","value2");
  6. HashMap<String,String> map2 = new HashMap<>();
  7. map2.put("key3","value3");
  8. map2.put("key4","value4");
  9. Gson gson = new Gson();
  10. String service = gson.toJson(map2);
  11. map1.put("service", service);
  12. String json = gson.toJson(map1);
  13. MyModel myModel = gson.fromJson(json, MyModel.class);
  14. System.out.println(gson.toJson(myModel));
  15. }
  16. 我的实体类
  17. public class MyModel {
  18. String key1;
  19. String key2;
  20. String service;
  21. public String getKey1() {
  22. return key1;
  23. }
  24. public void setKey1(String key1) {
  25. this.key1 = key1;
  26. }
  27. public String getKey2() {
  28. return key2;
  29. }
  30. public void setKey2(String key2) {
  31. this.key2 = key2;
  32. }
  33. public String getService() {
  34. return service;
  35. }
  36. public void setService(String service) {
  37. this.service = service;
  38. }
  39. }
  40. 有没有更高效的方法来实现这个
英文:

I have two HashMap I want to de-serialize to entity class using GSON.

  1. void convert(){
  2. HashMap&lt;String,String&gt; map1 = new HashMap&lt;&gt;();
  3. map1.put(&quot;key1&quot;,&quot;value1&quot;);
  4. map1.put(&quot;key2&quot;,&quot;value2&quot;);
  5. HashMap&lt;String,String&gt; map2 = new HashMap&lt;&gt;();
  6. map2.put(&quot;key3&quot;,&quot;value3&quot;);
  7. map2.put(&quot;key4&quot;,&quot;value4&quot;);
  8. Gson gson = new Gson();
  9. String service = gson.toJson(map2);
  10. map1.put(&quot;service&quot;, service);
  11. String json = gson.toJson(map1);
  12. MyModel myModel = gson.fromJson(json, MyModel.class);
  13. System.out.println(gson.toJson(myModel));
  14. }

My entity class

  1. public class MyModel {
  2. String key1;
  3. String key2;
  4. String service;
  5. public String getKey1() {
  6. return key1;
  7. }
  8. public void setKey1(String key1) {
  9. this.key1 = key1;
  10. }
  11. public String getKey2() {
  12. return key2;
  13. }
  14. public void setKey2(String key2) {
  15. this.key2 = key2;
  16. }
  17. public String getService() {
  18. return service;
  19. }
  20. public void setService(String service) {
  21. this.service = service;
  22. }
  23. }

Is there any efficient approach to do it.

答案1

得分: 1

你可以直接将 HashMap 转换为 JSON:

  1. HashMap<String, Object> map1 = new HashMap<>();
  2. map1.put("key1", "value1");
  3. map1.put("key2", "value2");
  4. HashMap<String, String> map2 = new HashMap<>();
  5. map2.put("key3", "value3");
  6. map2.put("key4", "value4");
  7. map1.put("service", map2);
  8. String result = gson.toJson(map1);

如果你想将 JSON 转换为实体类(Entity class):

  1. MyModel myModel = gson.fromJson(result, MyModel.class);

并且在 service 字段中使用 Map<String, String>

  1. public class MyModel {
  2. String key1;
  3. String key2;
  4. Map<String, String> service;
  5. // ...
  6. }
英文:

You can directly convert HashMap to json

  1. HashMap&lt;String,Object&gt; map1 = new HashMap&lt;&gt;();
  2. map1.put(&quot;key1&quot;,&quot;value1&quot;);
  3. map1.put(&quot;key2&quot;,&quot;value2&quot;);
  4. HashMap&lt;String,String&gt; map2 = new HashMap&lt;&gt;();
  5. map2.put(&quot;key3&quot;,&quot;value3&quot;);
  6. map2.put(&quot;key4&quot;,&quot;value4&quot;);
  7. map1.put(&quot;service&quot;, map2);
  8. String result = gson.toJson(map1);

And if you want convert json to Entity class

  1. MyModel myModel = gson.fromJson(result, MyModel.class);

And use Map&lt;String,String&gt; for service field

  1. public class MyModel {
  2. String key1;
  3. String key2;
  4. Map&lt;String,String&gt; service;
  5. ...
  6. }

huangapple
  • 本文由 发表于 2020年8月8日 16:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63313094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定