映射 JSON 到列表<Map<<String, Object>>
Map JSON To List<Map<<String, Object>>
我有一个JSON格式
[{
"id" : "a01",
"name" : "random1",
"val" : "random2"
},
{
"id" : "a03",
"name" : "random3",
"val" : "random4"
}]
我需要将它映射到一个 List
持有各种 Map
对象。我如何实现它?
即使我能够将此 JSON 转换为 String
的 List
形式
{
"id" : "a01",
"name" : "random1",
"val" : "random2"
}
然后我有一种方法可以将每个 String
转换为 Map
。
使用指定类型的 gson 转换为地图列表:
Gson gson = new Gson();
Type resultType = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> result = gson.fromJson(json, resultType);
您需要将 TypeReference
传递给 readValue
并具有所需的结果类型:
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
私有列表
Json 你需要通过:-
{
"map_formula" : [
{
"A+" : "if(price<400),\"40000\",0",
"B" : "",
"c" : "",
"d" : "",
"e" : ""
},
{
"poor" : "value for poor",
"good" : "300",
"average" : "300",
"excellent" : "300"
}
]
}
我有一个JSON格式
[{
"id" : "a01",
"name" : "random1",
"val" : "random2"
},
{
"id" : "a03",
"name" : "random3",
"val" : "random4"
}]
我需要将它映射到一个 List
持有各种 Map
对象。我如何实现它?
即使我能够将此 JSON 转换为 String
的 List
形式
{
"id" : "a01",
"name" : "random1",
"val" : "random2"
}
然后我有一种方法可以将每个 String
转换为 Map
。
使用指定类型的 gson 转换为地图列表:
Gson gson = new Gson();
Type resultType = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> result = gson.fromJson(json, resultType);
您需要将 TypeReference
传递给 readValue
并具有所需的结果类型:
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
私有列表
Json 你需要通过:-
{
"map_formula" : [
{
"A+" : "if(price<400),\"40000\",0",
"B" : "",
"c" : "",
"d" : "",
"e" : ""
},
{
"poor" : "value for poor",
"good" : "300",
"average" : "300",
"excellent" : "300"
}
]
}