使用Jackson反序列化JSON时出错:无法从START_ARRAY令牌中反序列化java.util.LinkedHashMap的实例

时间:2014-04-25 14:15:49

标签: java json jackson deserialization

我试图将json反序列化为MyClass对象,但每次我得到异常,但我能够序列化JSON对象。

**com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token**

我试图反序列化的json是

 {
  "siteName": "avisports",
  "csAssetTypes": [
    "Content_C",
    [
      "name",
      "description",
      "subheadline",
      "abstract",
      "body",
      "headline",
      "subheadline",
      "avi_content_title",
      "avi_content_body",
      "avi_content_headline",
      "avi_content_abstract"
    ]
  ]
}

我的Bean就像

public class GlContentServerConfig{

    private String siteName;
    private Map<String, List<String>> csAssetTypes = new HashMap<String, List<String>>();

    //getters ans setters
}

我的服务方法是

@Override
    public GlContentServerConfig getConfiguredAttributes(String siteName, String assetType) throws Exception {
        if (this.configMap == null) {
            this.configMap = this.configDAO.getAllConfigs();
        }

        GlContentServerConfig config = new GlContentServerConfig();
        config.setSiteName(siteName);               

        config.setCsAssetTypes(csService.getAssetTypeAttributeList(assetType));

        GloballinkConfig obj = this.configMap.get(siteName);
        if (obj != null) {
            String jsonValue = obj.getGlConfigValue();

            config=this.mapper.readValue(jsonValue, GlContentServerConfig.class); // error comes from this line
            //List<GlContentServerConfig> glconfigList= this.mapper.readValue(jsonValue, new TypeReference<GlContentServerConfig>(){});
            //List<GlContentServerConfig> glconfigList = this.mapper.readValue(jsonValue, this.mapper.getTypeFactory().constructCollectionType(List.class, GlContentServerConfig.class));

            System.out.println("final : "+glconfigList.toString());

        }

        return config;
    }

我已经尝试了所有的排列和组合。其中很少有人在代码中发表评论。 我无法弄清楚如何使用杰克逊。请帮帮我。

1 个答案:

答案 0 :(得分:0)

@JsonProperty("csAssetTypes")添加到您的Bean:

public class GlContentServerConfig{

    private String siteName;
@JsonProperty("csAssetTypes") 
    private Map<String, List<String>> csAssetTypes = new HashMap<String, List<String>>();

    //getters ans setters
}