英文:
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 = "{
"colmdl":[{"field":"srrno"},
{"field":"loadport"}],
"suppressClipboardPaste":true,
"excelSheetName":"Grid_sheet",
"editable":true,
"excelExportType":"Toolbar"
}"
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<String,Object>
。
以下是使用Jackson库的ObjectMapper的示例。
String json = "{ ... }"
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){})
英文:
If you still looking for a map, it should be Map<String,Object>
.
Here is example using ObjectMapper from Jackson library.
String json = "{ ... }"
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(json,new TypeReference<Map<String, Object>>(){})
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论