如何将字符串转换为哈希映射

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

How to convert String into hashmap

问题

  1. String a = "{
  2. \"colmdl\":[{\"field\":\"srrno\"},
  3. {\"field\":\"loadport\"}],
  4. \"suppressClipboardPaste\":true,
  5. \"excelSheetName\":\"Grid_sheet\",
  6. \"editable\":true,
  7. \"excelExportType\":\"Toolbar\"
  8. }";
  9. // 将上述字符串转换为一个映射(Map)
  10. Map<String, Object> map = new Gson().fromJson(a, new TypeToken<Map<String, Object>>() {}.getType());
  11. // 获取映射中的对应值
  12. Object colmdl = map.get("colmdl");
  13. Object suppressClipboardPaste = map.get("suppressClipboardPaste");
  14. Object excelSheetName = map.get("excelSheetName");
  15. Object editable = map.get("editable");
  16. Object excelExportType = map.get("excelExportType");
英文:
  1. String a = &quot;{
  2. &quot;colmdl&quot;:[{&quot;field&quot;:&quot;srrno&quot;},
  3. {&quot;field&quot;:&quot;loadport&quot;}],
  4. &quot;suppressClipboardPaste&quot;:true,
  5. &quot;excelSheetName&quot;:&quot;Grid_sheet&quot;,
  6. &quot;editable&quot;:true,
  7. &quot;excelExportType&quot;:&quot;Toolbar&quot;
  8. }&quot;

this is I stored in as a string , i need to set this as a map.

for ex colmdl,suppressClipboardPaste,excelSheetName,editable, excelExportType these all should be key.

答案1

得分: 1

如果您仍然在寻找地图,它应该是 Map&lt;String,Object&gt;
以下是使用Jackson库的ObjectMapper的示例。

  1. String json = &quot;{ ... }&quot;
  2. ObjectMapper mapper = new ObjectMapper();
  3. Map&lt;String, Object&gt; map = mapper.readValue(json, new TypeReference&lt;Map&lt;String, Object&gt;&gt;(){})
英文:

If you still looking for a map, it should be Map&lt;String,Object&gt;.
Here is example using ObjectMapper from Jackson library.

  1. String json = &quot;{ ... }&quot;
  2. ObjectMapper mapper = new ObjectMapper();
  3. Map&lt;String, Object&gt; map = mapper.readValue(json,new TypeReference&lt;Map&lt;String, Object&gt;&gt;(){})

答案2

得分: 0

那是 JSON 数据,因此您需要一个 JSON 解析工具。您需要一个库来完成这个任务。例如,Jackson

英文:

That's JSON data, so you need a JSON reading tool. You need a library to do this. For example, Jackson.

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

发表评论

匿名网友

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

确定