无法从 START_ARRAY 令牌反序列化 java.util.HashMap 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36519974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can not deserialize instance of java.util.HashMap out of START_ARRAY token
提问by Ashish
I am facing issue while parsing JSON using Hymanson-core-2.7.3.jar You can get them from here http://repo1.maven.org/maven2/com/fasterxml/Hymanson/core/
我在使用 Hymanson-core-2.7.3.jar 解析 JSON 时遇到问题您可以从这里获取它们http://repo1.maven.org/maven2/com/fasterxml/Hymanson/core/
My JSON File is
我的 JSON 文件是
[
{
"Name": "System Idle Process",
"CreationDate": "20160409121836.675345+330"
},
{
"Name": "System",
"CreationDate": "20160409121836.675345+330"
},
{
"Name": "smss.exe",
"CreationDate": "20160409121836.684966+330"
}
]
and the Java Code is by which I am trying to parse this is
我试图解析的Java代码是
byte[] mapData = Files.readAllBytes(Paths.get("process.txt"));
Map<String,String> myMap = new HashMap<String, String>();
ObjectMapper objectMapper=new ObjectMapper();
myMap = objectMapper.readValue(mapData, HashMap.class);
System.out.println("Map is: "+myMap);
But upon execution I am getting the error
但是在执行时我收到错误
com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token
at [Source: [B@34ce8af7; line: 1, column: 1]
at com.fasterxml.Hymanson.databind.JsonMappingException.from(JsonMappingException.java:216)
at com.fasterxml.Hymanson.databind.DeserializationContext.mappingException(DeserializationContext.java:873)
at com.fasterxml.Hymanson.databind.DeserializationContext.mappingException(DeserializationContext.java:869)
at com.fasterxml.Hymanson.databind.deser.std.StdDeserializer._deserializeFromEmpty(StdDeserializer.java:874)
at com.fasterxml.Hymanson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:337)
at com.fasterxml.Hymanson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
at com.fasterxml.Hymanson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.Hymanson.databind.ObjectMapper.readValue(ObjectMapper.java:2872)
I have tried searching over stackoverflow but couldnot find a matchable solution to this type of JSON.
我曾尝试在 stackoverflow 上进行搜索,但找不到与此类 JSON 匹配的解决方案。
Any help would be appreciated.
任何帮助,将不胜感激。
NOTE: This
JSON
mentioned here is different aJSON
withoutKey
, for the first element it has value directly and inside that value it haskey:value
pair. I am not sure how do I accesskey:value
pair which is inside a value.
注意:这里
JSON
提到的是不同的 aJSON
withoutKey
,对于第一个元素,它直接具有值,并且在该值内它具有key:value
对。我不确定如何访问key:value
值内的对。
采纳答案by Vikrant Kashyap
Create a simple pojo
Class First
pojo
首先创建一个简单的类
class MyClass
{
@JsonProperty
private String Name;
@JsonProperty
private String CreationDate;
}
and use this code...
并使用此代码...
byte[] mapData = Files.readAllBytes(Paths.get("process.txt"));
ObjectMapper objectMapper=new ObjectMapper();
//add this line
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<MyClass> myObjects = mapper.readValue(mapData , new TypeReference<List<MyClass>>(){});
or
或者
byte[] mapData = Files.readAllBytes(Paths.get("process.txt"));
ObjectMapper objectMapper=new ObjectMapper();
//add this line
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<MyClass> myObjects = mapper.readValue(mapData , mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
myObjects
will contains the List
of MyClass
. Now you can access this list as per your requirement.
myObjects
将包含List
的MyClass
。现在,您可以根据需要访问此列表。
回答by KrishnaSingh
It seem's that your file is representing a List of objects with Nameand CreationDatefields.
您的文件似乎表示具有Name和CreationDate字段的对象列表。
So you have to just use List instead of HashMap to ObjectMapper, code is mentioned below:
所以你只需要使用 List 而不是 HashMap 到 ObjectMapper,代码如下:
List<HashMap> dataAsMap = objectMapper.readValue(mapData, List.class);
回答by freedev
This exception is raised because you're trying to deserialize a List
in a Map
.
引发此异常是因为您试图反序列化 aList
中的 a Map
。
The solution is create a TypeReference of List<Map<String, Object>>
:
解决方案是创建一个 TypeReference List<Map<String, Object>>
:
List<Map<String, Object>> myObjects =
mapper.readValue(mapData , new TypeReference<List<Map<String, Object>>>(){});
回答by Guru
Well, you are trying to convert an json stringthat contains array/list of objectsinto an object.
好吧,您正在尝试将包含对象数组/列表的json 字符串转换为object。
Ex: { "key": "value"}
is the json you want to convert, but actually you are doing,
例如:{ "key": "value"}
是您要转换的json,但实际上您正在做,
[{ "key": "value"}]
.
[{ "key": "value"}]
.
simple fix, is remove the first and last char from your string and try. Hope it helps;)
简单的解决方法是从字符串中删除第一个和最后一个字符并尝试。希望能帮助到你;)
回答by alexandrubarbat
Why not?
为什么不?
ObjectMapper mapper = new ObjectMapper();
Object obj = mapper.readValue(file, new TypeReference<Object>() {});