将 JSON 字符串映射到 HashMap

考虑以下 JSON:


[

  {

    "map": "TEST",

    "values": [

      "test",

      "test2"

    ]

  },

  {

    "map": "TEST1",

    "values": [

      "test",

      "test3",

      "test4"

    ]

  },

  {

    "map": "TEST2",

    "values": [

      "test4",

      "test2",

      "test5",

      "test2"

    ]

  }

]

它们已被 getResourceAsString 函数加载到字符串中。如何制作一个 HashMap,其中我的键是“map”字段,而我的值是“values”字段数组?我在其他类似问题中尝试了很多解决方案,但没有任何效果。


这是我的代码的开头:


    ObjectMapper mapper = new ObjectMapper();

    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

但我不知道如何将它分配给 Map,readValue 方法似乎没有给出正确的东西


Helenr
浏览 134回答 3
3回答

温温酱

您可以将其反序列化为List<Map<String, Object>>,然后转换为Map:import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.ObjectMapper;import java.io.File;import java.util.HashMap;import java.util.List;import java.util.Map;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; File jsonFile = new File("./src/main/resources/test.json");&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; TypeReference rootType = new TypeReference<List<Map<String, Object>>>() { };&nbsp; &nbsp; &nbsp; &nbsp; List<Map<String, Object>> root = mapper.readValue(jsonFile, rootType);&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> result = root.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;m -> m.get("map").toString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;m -> m.get("values")));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(result);&nbsp; &nbsp; }}上面的代码打印:{TEST2=[test4, test2, test5, test2], TEST=[test, test2], TEST1=[test, test3, test4]}

元芳怎么了

你可以这样做:ArrayNode rootNode = (ArrayNode) new ObjectMapper().readTree(...);Map<String, List<String>> map = new LinkedHashMap<>();for (int i = 0; i < rootNode.size(); i++) {&nbsp; &nbsp; JsonNode objNode = rootNode.get(i);&nbsp; &nbsp; String name = objNode.get("map").textValue();&nbsp; &nbsp; ArrayNode valuesNode = (ArrayNode) objNode.get("values");&nbsp; &nbsp; List<String> values = new ArrayList<>(valuesNode.size());&nbsp; &nbsp; for (int j = 0; j < valuesNode.size(); j++)&nbsp; &nbsp; &nbsp; &nbsp; values.add(valuesNode.get(j).textValue());&nbsp; &nbsp; map.put(name, values);}结果{TEST=[test, test2], TEST1=[test, test3, test4], TEST2=[test4, test2, test5, test2]}

宝慕林4294392

您可以List<Map<Object,Object>>将此 json 用作 3 个对象的列表,因此它不能直接转换为 a&nbsp;HashMap,因此请尝试以下操作:String&nbsp;json&nbsp;=&nbsp;"[{\"map\":\"TEST\",\"values\":[\"test\",\"test2\"]},{\"map\":\"TEST1\",\"values\":[\"test\",\"test3\",\"test4\"]},{\"map\":\"TEST2\",\"values\":[\"test4\",\"test2\",\"test5\",\"test2\"]}]"; List<Map<Object,&nbsp;Object>>&nbsp;jsonObj&nbsp;=&nbsp;mapper.readValue(json,&nbsp;List.class);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java