【问题标题】:Put hash map value arrayincorrect放置 hashmap 值数组不正确
【发布时间】:2018-01-24 09:25:21
【问题描述】:

我已将值 json 数组放入哈希映射,但最后一个值是正确的。我仍然不清楚使用哈希映射、列表和数组列表。我的代码:

Map<String, Object> departmentPHSSuportEmail = new HashMap<String, Object>();
Map<String, Object> subSourceMap = null;

List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();

subSourceMap = new HashMap<>();
subSourceMap.put("name", "bobby");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");

subSourceMap.put("name", "2sadasd");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");      

subSourceMap.put("name", "ggfgf");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");      

myMap.add(0, subSourceMap);
myMap.add(1, subSourceMap);
myMap.add(2, subSourceMap);


Map<String, Object> attachment = new HashMap<String, Object>();
attachment.put(ApplicationConstanta.MsExchange.TYPE, "");
attachment.put(ApplicationConstanta.MsExchange.VALUE, "");
departmentPHSSuportEmail.put(ApplicationConstanta.EmailForwardString.ATTACHMENT, attachment);

Map<String, Object> subSource = new HashMap<String, Object>();
subSource.put(ApplicationConstanta.MsExchange.TYPE, ApplicationConstanta.MsExchange.STRING);
subSource.put(ApplicationConstanta.MsExchange.VALUE, myMap);
departmentPHSSuportEmail.put(ApplicationConstanta.EmailForwardString.SUB_SOURCE, subSource);

System.out.println("JSON : " + JsonUtil.toJson(departmentPHSSuportEmail));

结果:

JSON : {
  "Attachment" : {
    "type" : "",
    "value" : ""
  },
  "SubSource" : {
    "type" : "string",
    "value" : [ {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "ggfgf"
    }, {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "ggfgf"
    }, {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "ggfgf"
    } ]
  }
}

为什么“值”对象中的值都一样?我已经提出了不同的价值。如何解决这个问题呢?谢谢

【问题讨论】:

  • 您在myMap 中添加了三次subSourceMap。如果要添加三张不同的地图,则需要添加三张不同的地图,而不是三张地图。
  • 如果你不熟悉 hashmaps、lists 等是如何工作的,那么你应该专注于这些,而不是把 json 弄乱。 subSourceMap.put("name", whatever); 将覆盖您之前为键 "name" 设置的任何值。这意味着 subSourceMap 将仅包含最后一个值,并且将 same 映射 3 次添加到列表中只会导致:具有 3 个完全相同的元素的列表(并且当您更改您更改所有内容的内容)。

标签: java json hashmap


【解决方案1】:

您看到这个问题是因为您重复使用了基本上会覆盖您的数据的相同 Hashmap 对象。让我们一步一步来:

List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();

subSourceMap = new HashMap<>();
subSourceMap.put("name", "bobby");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");

现在您的subSourceMap 包含:

name: bobby
address: xxx.xxxx@xxx.co.id

那你就做

subSourceMap.put("name", "2sadasd");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");  

使用此代码,您可以修改现有的subSourceMap 并覆盖它的内容。现在你的subSourceMap 包含:

name: 2sadasd
address: xxx.xxxx@xxx.co.id

然后你做:

subSourceMap.put("name", "ggfgf");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");

您再次修改现有的subSourceMap。现在你的subSourceMap 包含:

name: ggfgf
address: xxx.xxxx@xxx.co.id

你终于做到了:

myMap.add(0, subSourceMap);
myMap.add(1, subSourceMap);
myMap.add(2, subSourceMap);

这会将对同一subSourceMap 的三个引用放入列表中。它不会复制地图,它只是在其中添加对同一对象的引用。因此,当您现在将此列表转换为 JSON 时,您自然会看到您所看到的。

要解决此问题,您需要为每个条目创建一个新地图:

List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();

// create a fresh map
Map<String,Object> subSourceMap1 = new HashMap<>();
subSourceMap1.put("name", "bobby");
subSourceMap1.put("address", "xxx.xxxx@xxx.co.id");

// create a fresh map
Map<String,Object> subSourceMap2 = new HashMap<>();
subSourceMap2.put("name", "2sadasd");
subSourceMap2.put("address", "xxx.xxxx@xxx.co.id");      

// create a fresh map
Map<String,Object> subSourceMap3 = new HashMap<>();
subSourceMap3.put("name", "ggfgf");
subSourceMap3.put("address", "xxx.xxxx@xxx.co.id");      

// note that you do not need to specify the index here, just use
// add with 1 parameter which will append to the end of the list
myMap.add(subSourceMap1);
myMap.add(subSourceMap2);
myMap.add(subSourceMap3);

您可以阅读这篇关于 Java 引用的文章,其中显示了一个类似的示例,以更熟悉 Java 中对象引用的工作原理:http://www.informit.com/articles/article.aspx?p=174371&seqNum=4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多