英文:
Object type remains as LinkedHashMap after assignment
问题
我有以下JSON数据从外部文件源获取:
{
.... JSON格式中的更复杂结构,但我只关心以下部分
"feature": {
"100": {
"DATA1": [
{
"header": "head1",
"someId": "123"
}
]
}
}
}
我尝试捕获以下部分作为List <Data>
:
"DATA1": [
{
"header": "head1",
"someId": "123"
}
]
但无法做到。出现以下错误:
> 无法将LinkedHashMap转换为List。
获取数据并尝试将其分配给List<Data>
的方法:
private void getData(){
AllDataFromFile all = someFetchAPI(); // 从文件中获取了每个JSON数据。
// 获取我想要的部分,我确实得到了它。
Map<String, Map<String,Object>> feature = all.getFeature();
Map<String, Object> allData = feature.get("100");
List<Data> dataList = allData.get("DATA1");
}
以上既没有编译错误也没有运行时错误,但dataList
不是List<Data>
。
实际上,在调试模式下,它是一个LinkedHashMap的列表。为什么?如何将其转换为List<Data>
?
由于它不能映射为List<Data>
,因此我无法执行以下操作。
dataList.get(0).getHeader().
无法将其强制转换,也出现相同错误:
> 无法将LinkedHashMap转换为List。
请给予建议。谢谢。
AllDataFromFile
类:
@Getter
@Setter
@JsonInclude(Include.NON_NULL)
public class AllDataFromFile {
private Map<String, Map<String,Object>> feature;
}
Data
类:
@JsonInclude(Include.NON_NULL)
public class Data implements Comparable<Data>, Cloneable{
private String header;
private String someId;
@Override
public int compareTo(Data o) {
int result = // 一些逻辑
return result;
}
}
编辑:
这是List<Data>
内部的数据格式:
result = {ArrayList@18590} size = 1
0 = {LinkedHashMap@18593} size = 2
"header" -> "header1"
"someId" -> "id1"
当我尝试将对象的值与更具体的类型匹配时,会出现以下错误。
private Map<String, Map<String, List<Data>>> feature;
如果我尝试以下内容,将回到获取LinkedHashMap的情况(没有用,回到起点):
private Map<String, Map<String, List>> feature;
错误:
> 无法读取JSON:Class java.util.LinkedHashMap不是 [简单类型,类com.a.a.a.a.a.Data] 的子类型(通过引用链:com.b.b.b.b.b.b.AllDataFromFile["feature"]->java.util.LinkedHashMap["100"]->java.util.LinkedHashMap["DATA1"]->java.util.ArrayList[0])。
英文:
I have the following JSON data being fetched from an external file source.
{
.... More complicated structures within this JSON format above but I only care about the following
"feature": {
"100": {
"DATA1": [
{
"header": "head1",
"someId": "123"
}
]
}
}
}
I am trying to capture the following portion as List < Data >
"DATA1": [
{
"header": "head1",
"someId": "123"
}
]
but unable to do so. Getting following error:
> Cannot cast LinkedHashMap to List.
Method which fetched the data and try to assign it to List< Data >
private void getData(){
AllDataFromFile all = someFetchAPI(); // Got every JSON data in the file.
// capturing the portion that I want which I do get.
Map<String, Map<String,Object>> feature = all.getFeature();
Map<String, Object> allData = feature.get("100");
List<Data> dataList = allData.get("DATA1");
}
No compilation nor run time errors from above but dataList is not a List of Data.
Instead it is a List of LinkedHashMap when I see in debug mode. Why?
And how can I turn this into a List of Data instead?
Since it doesn't map as a List< Data >, I am unable to perform operations such as follows.
dataList.get(0).getHeader().
Unable to cast it either and getting same error:
> Cannot cast LinkedHashMap to List.
Please advice. Thanks.
AllDataFromFile class
@Getter
@Setter
@JsonInclude(Include.NON_NULL)
public class AllDataFromFile {
private Map<String, Map<String,Object>> feature;
}
Data class
@JsonInclude(Include.NON_NULL)
public class Data implements Comparable<Data>, Cloneable{
private String header;
private String someId;
@Override
public int compareTo(Data o) {
int result = // some logic
return result;
}
}
EDIT:
This is data format inside List<Data>
result = {ArrayList@18590} size = 1
0 = {LinkedHashMap@18593} size = 2
"header" -> "header1"
"someId" -> "id1"
Getting following error when I try to match the Object's value to be more specific as follows.
private Map<String, Map<String, List<Data>>> feature;
Able to go back to getting LinkedHashMap (no use, back to Square one) if I go for the following:
private Map<String, Map<String, List>> feature;
Error:
> Could not read JSON: Class java.util.LinkedHashMap not subtype of
> [simple type, class com.a.a.a.a.a.Data] (through reference chain:
> com.b.b.b.b.b.b.AllDataFromFile["feature"]->java.util.LinkedHashMap["100"]->java.util.LinkedHashMap["DATA1"]->java.util.ArrayList[0])
答案1
得分: 1
以下是翻译好的内容:
Jackson使用反射来确定它需要反序列化的类型。
但是从AllDataFromFile开始,它无法确定内容实际上是一个Data对象列表,因此它会退回到默认的列表(ArrayList)和映射(LinkedHashMap)。
我认为如果您在类型声明中提供更多信息,Jackson可能会弄清楚:
public class AllDataFromFile {
private Map<String, Map<String, List<Data>>> feature;
}
英文:
Jackson uses reflection to determine what type it needs to deserialize too.
But starting from AllDataFromFile, it cannot determine that the content is actually a List of Data objects, so it falls back to default Lists (ArrayList) and Maps(LinkedHashMap).
I think if you provide more information in the type declaration, jackson might figure it out:
public class AllDataFromFile {
private Map<String, Map<String, List<Data>>> feature;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论