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

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

How to convert String into hashmap

问题

String a = "{
    \"colmdl\":[{\"field\":\"srrno\"},
    	{\"field\":\"loadport\"}],
    \"suppressClipboardPaste\":true,
    \"excelSheetName\":\"Grid_sheet\",
    \"editable\":true,
    \"excelExportType\":\"Toolbar\"
}";

// 将上述字符串转换为一个映射(Map)
Map<String, Object> map = new Gson().fromJson(a, new TypeToken<Map<String, Object>>() {}.getType());

// 获取映射中的对应值
Object colmdl = map.get("colmdl");
Object suppressClipboardPaste = map.get("suppressClipboardPaste");
Object excelSheetName = map.get("excelSheetName");
Object editable = map.get("editable");
Object excelExportType = map.get("excelExportType");
英文:
String a = &quot;{
 &quot;colmdl&quot;:[{&quot;field&quot;:&quot;srrno&quot;},
	{&quot;field&quot;:&quot;loadport&quot;}],
 &quot;suppressClipboardPaste&quot;:true,
 &quot;excelSheetName&quot;:&quot;Grid_sheet&quot;,
 &quot;editable&quot;:true,
 &quot;excelExportType&quot;:&quot;Toolbar&quot;
}&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的示例。

String json = &quot;{ ... }&quot;
ObjectMapper mapper = new ObjectMapper();
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.

String json = &quot;{ ... }&quot;
ObjectMapper mapper = new ObjectMapper();
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:

确定