Java - 使用jakson将json转换为复杂对象

时间:2018-02-15 06:19:47

标签: java json jackson

这可能是重复的问题,但我已经搜索了很多解决方案,但没有运气。 我有一个类如下:

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; }
}

当我使用以下代码尝试使用json时,我正在获取json文件。

RestartMapState vRestartMapState = new RestartMapState(vRemainingStepsToBeExecuted, vMapResolvedParameters, vMapExCtx.getvJobID());
try {
    vObjectMapper.writerWithDefaultPrettyPrinter().writeValue(stateFile, vRestartMapState);
} catch (JsonGenerationException e) {
    isSerialized = false;
    e.printStackTrace();
} catch (JsonMappingException e) {
    isSerialized = false;
    e.printStackTrace();
} catch (IOException e) {
    isSerialized = false;
    e.printStackTrace();
}

我的JSON文件如下所示:

{
  "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;

有人可以帮我解决这个问题,我做错了什么?

2 个答案:

答案 0 :(得分:2)

AbstractMap.SimpleEntry没有默认构造函数(没有参数),无法反序列化 您应该将vResolvedParameters的通用类型从AbstractMap.SimpleEntry<String,String>更改为其他类型:

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;
}

Pair的位置:

private static class Pair<K, V> {
    private K key;
    private V value;

    public K getKey() {
        return key;
    }
    public void setKey(K key) {
        this.key = key;
    }
    public V getValue() {
        return value;
    }
    public void setValue(V value) {
        this.value = value;
    }
    ...
}

您可以使用许多其他库中的Pair实现。

如果"key"的值是唯一的,您可以将vResolvedParameters类型更改为Map<String,String>。但是您的JSON格式将从以下更改:

...
"vResolvedParameters" : [ {
  "key" : "##RUNDATE##",
  "value" : "2018045"
}, {
  "key" : "##julian##",
  "value" : "2018033"
} ],
...

为:

...
"vResolvedParameters": {
  "##RUNDATE##": "2018045",
  "##julian##": "2018033"
},
...

Related qestion

答案 1 :(得分:0)

最后我解决了这个问题。问题在于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; }

谢谢@Nokolay。