Java-使用jakson将json转换为复杂对象
这可能会重复这个问题,但我已经寻找了很多解决方案,但并没有运气。 我的课程如下:Java-使用jakson将json转换为复杂对象,java,json,jackson,Java,Json,Jackson,这可能会重复这个问题,但我已经寻找了很多解决方案,但并没有运气。 我的课程如下: public class RestartMapState { public RestartMapState(List<MapStepData> vMapStepData, List<AbstractMap.SimpleEntry<String,String>> vResolvedParameters, String vJobID){ this.vJobI
public class RestartMapState {
public RestartMapState(List<MapStepData> vMapStepData, List<AbstractMap.SimpleEntry<String,String>> vResolvedParameters, String vJobID){
this.vJobID=vJobID;
this.vResolvedParameters = vResolvedParameters;
this.vMapStepData = vMapStepData;
}
public RestartMapState(){}
private List<MapStepData> vMapStepData;
public void setvMapStepData(List<MapStepData> mapStepData){ this.vMapStepData = mapStepData; }
public List<MapStepData> getvMapStepData(){
return this.vMapStepData;
}
private List<AbstractMap.SimpleEntry<String,String>> vResolvedParameters;
public List<AbstractMap.SimpleEntry<String,String>> getvResolvedParameters(){ return this.vResolvedParameters; }
public void setvResolvedParameters(List<AbstractMap.SimpleEntry<String,String>> resolvedParameters){ this.vResolvedParameters = resolvedParameters; }
private String vJobID;
public String getvJobID(){ return this.vJobID; }
public void setvJobID(String jobID){ this.vJobID = jobID; }
}
{
"vMapStepData" : [ {
"dependsOn" : null,
"orderIndex" : 0,
"name" : "Step1",
"mapStepId" : 167119,
"sqlstatements" : {
"sqlstatement" : [ {
"value" : "-- Map Step (Step1)\nCREATE TABLE ##!sessionid##Step1\n AS\nSELECT currency_nbr AS currency_nbr,...",
"orderIndex" : 1,
"isQuery" : false,
"flags" : [ "PartOfCore" ]
} ]
}
}, {
"dependsOn" : null,
"orderIndex" : 1,
"name" : "Step2",
"mapStepId" : 237822,
"sqlstatements" : {
"sqlstatement" : [ {
"value" : "\n\n-- Map Step (Step2)\nCREATE TABLE ##!sessionid##Step2\n AS\nSELECT country_cd AS ....",
"orderIndex" : 1,
"isQuery" : false,
"flags" : [ "PartOfCore" ]
} ]
}
} ],
"vResolvedParameters" : [ {
"key" : "##RUNDATE##",
"value" : "2018045"
}, {
"key" : "##julian##",
"value" : "2018033"
} ],
"vJobID" : "10012"
}
我的JSON文件如下所示:
public class RestartMapState {
public RestartMapState(List<MapStepData> vMapStepData, List<AbstractMap.SimpleEntry<String,String>> vResolvedParameters, String vJobID){
this.vJobID=vJobID;
this.vResolvedParameters = vResolvedParameters;
this.vMapStepData = vMapStepData;
}
public RestartMapState(){}
private List<MapStepData> vMapStepData;
public void setvMapStepData(List<MapStepData> mapStepData){ this.vMapStepData = mapStepData; }
public List<MapStepData> getvMapStepData(){
return this.vMapStepData;
}
private List<AbstractMap.SimpleEntry<String,String>> vResolvedParameters;
public List<AbstractMap.SimpleEntry<String,String>> getvResolvedParameters(){ return this.vResolvedParameters; }
public void setvResolvedParameters(List<AbstractMap.SimpleEntry<String,String>> resolvedParameters){ this.vResolvedParameters = resolvedParameters; }
private String vJobID;
public String getvJobID(){ return this.vJobID; }
public void setvJobID(String jobID){ this.vJobID = jobID; }
}
{
"vMapStepData" : [ {
"dependsOn" : null,
"orderIndex" : 0,
"name" : "Step1",
"mapStepId" : 167119,
"sqlstatements" : {
"sqlstatement" : [ {
"value" : "-- Map Step (Step1)\nCREATE TABLE ##!sessionid##Step1\n AS\nSELECT currency_nbr AS currency_nbr,...",
"orderIndex" : 1,
"isQuery" : false,
"flags" : [ "PartOfCore" ]
} ]
}
}, {
"dependsOn" : null,
"orderIndex" : 1,
"name" : "Step2",
"mapStepId" : 237822,
"sqlstatements" : {
"sqlstatement" : [ {
"value" : "\n\n-- Map Step (Step2)\nCREATE TABLE ##!sessionid##Step2\n AS\nSELECT country_cd AS ....",
"orderIndex" : 1,
"isQuery" : false,
"flags" : [ "PartOfCore" ]
} ]
}
} ],
"vResolvedParameters" : [ {
"key" : "##RUNDATE##",
"value" : "2018045"
}, {
"key" : "##julian##",
"value" : "2018033"
} ],
"vJobID" : "10012"
}
但是,当我尝试将同一个json文件反序列化为RestartMapState类型的对象时,我得到了以下错误
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class java.util.AbstractMap$SimpleEntry<java.lang.String,java.lang.String>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: C:\Users\vkukk1\.MAP_STATE_System_Test_Group_Vasu_Test_2_10012.json; line: 30, column: 5] (through reference chain: com.aexp.idn.magellan.RestartMapState["vResolvedParameters"]) RestartMapState vRestartMapState = null;
org.codehaus.jackson.map.JsonMappingException:找不到适合类型[simple type,class java.util.AbstractMap$SimpleEntry]的构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)
在[Source:C:\Users\vkukkk1\.MAP\u STATE\u System\u Test\u Group\u Vasu\u Test\u 2\u 10012.json;第30行,第5列](通过引用链:com.aexp.idn.magelan.RestartMapState[“vResolvedParameters”])RestartMapState vRestartMapState=null;
有人能帮我解决这个问题吗?我做错了什么?类
抽象映射。SimpleEntry
没有默认构造函数(没有参数),无法反序列化。您应该将
vResolvedParameters
的泛型类型从AbstractMap.SimpleEntry
更改为其他类型:
private List<Pair<String, String>> vResolvedParameters;
public List<Pair<String, String>> getvResolvedParameters() {
return this.vResolvedParameters;
}
public void setvResolvedParameters(List<Pair<String, String>> resolvedParameters) {
this.vResolvedParameters = resolvedParameters;
}
您可以使用许多其他库中的对
实现
或者,如果“key”
的值是唯一的,您可以将vResolvedParameters
类型更改为Map
。但您的JSON格式将从:
...
"vResolvedParameters" : [ {
"key" : "##RUNDATE##",
"value" : "2018045"
}, {
"key" : "##julian##",
"value" : "2018033"
} ],
...
致:
我终于解决了这个问题。问题是关于vResolvedParameter的getter方法。我将getter方法更改如下:
private List<MapStepData> vMapStepData;
public void setvMapStepData(List<MapStepData> mapStepData){ this.vMapStepData = mapStepData; }
public List<MapStepData> getvMapStepData(){ return this.vMapStepData; }
private Map<String,String> vResolvedParameters;
public Map<String,String> getvResolvedParameters() { return this.vResolvedParameters; }
私有列表vMapStepData;
public void setvMapStepData(List mapStepData){this.vMapStepData=mapStepData;}
public List getvMapStepData(){返回this.vMapStepData;}
私有地图vResolvedParameters;
公共映射getvResolvedParameters(){返回this.vResolvedParameters;}
谢谢@Nokolay.即使我改为Map,我也会遇到以下异常:-org.codehaus.jackson.Map.JsonMappingException:无法在[Source:C:\Test_2_10012.json;line:28,column:6](通过引用链:com.aexp.idn.magellan.RestartMapState)反序列化java.util.LinkedHashMap的实例[“vResolvedParameters”])顺便说一句,我没有在任何地方使用LinkedHashMap,我不知道为什么我会得到
MapStepData
类的这个例外添加代码
private List<MapStepData> vMapStepData;
public void setvMapStepData(List<MapStepData> mapStepData){ this.vMapStepData = mapStepData; }
public List<MapStepData> getvMapStepData(){ return this.vMapStepData; }
private Map<String,String> vResolvedParameters;
public Map<String,String> getvResolvedParameters() { return this.vResolvedParameters; }