基本上我正在使用
编写JSON文件private void setupDictionaries() {
ObjectMapper mapper = new ObjectMapper();
ArrayNode arrayNode = mapper.createArrayNode();
JsonNode rootNode = mapper.createObjectNode();
ArrayList<String> myThing = new ArrayList<String>();
myThing.add("hi");
myThing.add(".");
itemsDict.put("cake", myThing);
JsonNode childNode1 = mapper.valueToTree(itemsDict);
((ObjectNode) rootNode).set("Jan", childNode1);
JsonNode childNode2 = mapper.createObjectNode();
((ObjectNode) rootNode).set("obj2", childNode2);
JsonNode childNode3 = mapper.createObjectNode();
((ObjectNode) rootNode).set("obj3", childNode3);
String jsonString;
try {
jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
System.out.println(jsonString);
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File(statsFile), jsonString);
} catch (JsonProcessingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在这个JSON文件中,我想要为所有12个月生成一个类似的字典,并且只加载我想要专门使用的字典。但是,由于这些不是简单的HashMaps,当我尝试加载json文件时,我得到了异常。装货代码:
private HashMap<String, List<String>> loadDict() {
ObjectMapper mapper = new ObjectMapper();
try {
HashMap<String, ArrayList<String>> map = mapper.readValue(new File(statsFile), new TypeReference<HashMap<String, ArrayList<String>>>() {});
//Object map = mapper.readValue(new File(statsFile), new TypeReference<Object>() {});
System.out.println(map.get("cake");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
例外:
com.fasterxml.jackson.databind.JsonMappingException:无法构造java.util.HashMap的实例:no String-argument构造函数/工厂方法从String值反序列化
我的JSON文件:
{ “Jan”:{ “蛋糕”:[“hi”,“。” ] }, “obj2”:{}, “obj3”:{} }
编辑:我想我找出了导致它的原因
String input = new String(Files.readAllBytes(Paths.get(statsFile)));
System.out.println(input);
String input1 = "{\r\n \"Jan\" : {\r\n \"cake\" : [ \"hi\", \".\" ]\r\n },\r\n \"obj2\" : { },\r\n \"obj3\" : { }\r\n}";
System.out.println(input1);
运行此代码,输入为:
“{\ r \ n \”Jan \“:{\ r \ n \”蛋糕\“:[\”hi \“,\”。\“] \ r \ n},\ r \ n&n&gt; ; \“obj2 \”:{},\ r \ n \“obj3 \”:{} \ r \ n}“ { “Jan”:{ “蛋糕”:[“hi”,“。” ] }, “obj2”:{}, “obj3”:{} } (基本上从一个文件中读取,所有这些都在一行上,其中在输入字符串中具有实际的json字符串,它很好且干净,分成多行) 现在我只需要弄清楚如何克服这个......
解决 我不得不将jsonString写入文件,而是编写rootNode
private void setupDictionaries() {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
ArrayNode arrayNode = mapper.createArrayNode();
JsonNode rootNode = mapper.createObjectNode();
ArrayList<String> myThing = new ArrayList<String>();
myThing.add("hi");
myThing.add(".");
itemsDict.put("cake", myThing);
JsonNode childNode1 = mapper.valueToTree(itemsDict);
((ObjectNode) rootNode).set("Jan", childNode1);
JsonNode childNode2 = mapper.createObjectNode();
((ObjectNode) rootNode).set("obj2", childNode2);
JsonNode childNode3 = mapper.createObjectNode();
((ObjectNode) rootNode).set("obj3", childNode3);
String jsonString;
try {
//jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
System.out.println(rootNode);
writer.writeValue(new File(statsFile), rootNode);
} catch (JsonProcessingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我可以简单地通过
查询private HashMap<String, List<String>> loadDict() {
ObjectMapper mapper = new ObjectMapper();
try {
System.out.println("...");
HashMap<String, HashMap<String, ArrayList<String>>> map = mapper.readValue(new File(statsFile), new TypeReference<HashMap<String, HashMap<String, ArrayList<String>>>>() {});
System.out.println(map.get("Jan").get("cake").get(0));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
谢谢大家
答案 0 :(得分:0)
问题在于您的TypeReference
。您的示例JSON类似于HashMap<String, HashMap<String, ArrayList<String>>>
对于HashMap<String, ArrayList<String>>
,JSON应该类似于
{&#34; Jan&#34; :[&#34; hi&#34;,&#34;。&#34; ],&#34; obj2&#34; :[],&#34; obj3&#34; :[]}
<强>更新强>
测试您的示例JSON:
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String input = "{ \"Jan\" : { \"cake\" : [ \"hi\", \".\" ] }, \"obj2\" : { }, \"obj3\" : { } }";
ObjectMapper mapper = new ObjectMapper();
HashMap<String, HashMap<String, ArrayList<String>>> map = mapper.readValue(input,
new TypeReference<HashMap<String, HashMap<String, ArrayList<String>>>>() {
});
System.out.println(map.get("Jan").get("cake").get(0));
}