无法使用 jackson 从 START_ARRAY 令牌中反序列化 java.lang.String 的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40362036/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:09:22  来源:igfitidea点击:

Can not deserialize instance of java.lang.String out of START_ARRAY token using Hymanson

javajsonHymanson

提问by Anji R

Hi below is my JSON String

嗨,下面是我的 JSON 字符串

{
  "addons": {
    "BANDWIDTH": [
      [
        "HSV-A1ABW-C",
        "BANDWIDTH",
        "vCloud Air",
        {
          "Monthly": {
            "1_99999": [
              "219.00",
              "HSV-A1ABW-12MT0-C1S",
              "vCloud Air - 12 monthly Payments"
            ]
          },
          "Prepaid": {
            "1_99999": [
              "2577.00",
              "HSV-A1ABW-12PT0-C1S",
              "vCloud Air - Internet Bandwidth Subscription - 12 month Prepaid"
            ]
          }
        }
      ]
    ]
  }
}

public class sample
{
   @JsonProperty("addons")
    public Addons addons;
   //getters and setters
}
 public class Addons
{
   @JsonProperty("BANDWIDTH")
    public List<List<String>> bANDWIDTH;
    //getters and setters
}


ObjectMapper objectMapper = new ObjectMapper();     
obj = objectMapper.readValue(json,sample.getClass());

when i run the , i am getting the following error Can not deserialize instance of java.lang.String out of START_OBJECT token here i have array has an array and in that array have an object along with values , how to parse this using Hymanson and java. Please help me Thanks in advance

当我运行时,我收到以下错误无法从 START_OBJECT 令牌中反序列化 java.lang.String 的实例,我有数组有一个数组,在该数组中有一个对象和值,如何使用 Hymanson 和爪哇。请帮助我 提前致谢

回答by zeppelin

You have declared BANDWIDTH to be of type List<List<String>> , while in your JSON it is a mixed list of 3 strings:

您已将 BANDWIDTH 声明为 List<List<String>> 类型,而在您的 JSON 中,它是 3 个字符串的混合列表:

"HSV-A1ABW-C",
"BANDWIDTH",
"vCloud Air"

and a complex object:

和一个复杂的对象:

{
    "Monthly": {
        "1_99999": [
        "219.00",
        "HSV-A1ABW-12MT0-C1S",
        "vCloud Air - 12 monthly Payments"
        ]
    },
    "Prepaid": {
        "1_99999": [
        "2577.00",
        "HSV-A1ABW-12PT0-C1S",
        "vCloud Air - Internet Bandwidth Subscription - 12 month Prepaid"
        ]
    }
}

hence the error

因此错误

And easiest "workaround" you can employ, is to just declare you BANDWIDTHproperty to be of type:

您可以采用的最简单的“解决方法”就是将BANDWIDTH属性声明为以下类型:

List<List<Object>> 

and let Hymanson deserialize it as a combination of Maps and Lists.

并让 Hymanson 将其反序列化为 Maps 和 Lists 的组合。