【问题标题】:Can not deserialize instance of com.advice.domain.family.Income out of START_ARRAY token↵ at无法从 START_ARRAY 令牌↵ 反序列化 com.advice.domain.family.Income 的实例
【发布时间】:2016-06-17 11:01:30
【问题描述】:

我有两个班级,一个是Income,另一个是Salary。我有一个看起来像这样的 json。

"income" : [
    {
        "_id" : 271234.0, 
        "type" : "salary", 
        "amount" : 100000.0, 
        "inception" : "11/8/1986", 
        "endsOn" : "11/8/2030", 
        "salary" : {
            "ctc" : 200000.0, 
            "basic" : 32000.0, 
            "pf" : 14000.0, 
            "gratuity" : 55000.0, 
            "paci" : 5000.0, 
            "sa" : 50000.0, 
            "mediclaim" : 50000.0
        }
    }
]

班级Income

public class Income {

    public static final String INCOME = "income";

    private Long id;

    @Size(max = 30)
    private String type;

    @Size(max = 10)
    private int amount;

//  @Size(max = 50)
    private String inception =null;

//  @Size(max = 10)
    private String endsOn =null;

    private Salary salary;
}

班级Salary:

public class Salary {

    private int ctc;  

    private int basic;

    private int pf;

    private int gratuity;

    private int paci;

    private int sa;

    private int mediclaim;

}

我隐藏了 Stackoverflow 的所有构造函数、setter 和 getter,但它们在我的代码中。我想使用此代码从 URL 中使用 ObjectMapper 将 Json 解析为 Object:

@RequestMapping(value = "/user/{cUsrId}/lc/{lcId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<?> addNewIncome( @PathVariable Long cUsrId, @PathVariable Long lcId,@RequestBody List<Income> income) {

    log.debug("REST request to save Profile by id: {},  lcId: {},lcId: {}, income: {}",  cUsrId,lcId,income);
    int success = engagementService.saveNewIncome(cUsrId, lcId, income);
    if(success ==0){
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }else{
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

但我收到此错误:

“无法读取文档:无法反序列化 com.advice.domain.family.Income out of START_ARRAY token↵ at [来源: java.io.PushbackInputStream@3d3298f9;行:1,列:2](通过 参考链:java.util.ArrayList[0]);嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:不能 反序列化 com.advice.domain.family.Income 的实例 [来源:java.io.PushbackInputStream@3d3298f9;START_ARRAY 令牌↵; 行:1,列:2](通过引用链:java.util.ArrayList[0])"

【问题讨论】:

    标签: angularjs mongodb spring-boot


    【解决方案1】:

    你有轻微的脱节。您所呈现的 JSON 实际上不是一个收入对象,因此它对您大喊大叫是正确的。收入对象只是 JSON 字符串的这一部分:

    {
            "_id" : 271234.0, 
            "type" : "salary", 
            "amount" : 100000.0, 
            "inception" : "11/8/1986", 
            "endsOn" : "11/8/2030", 
            "salary" : {
                "ctc" : 200000.0, 
                "basic" : 32000.0, 
                "pf" : 14000.0, 
                "gratuity" : 55000.0, 
                "paci" : 5000.0, 
                "sa" : 50000.0, 
                "mediclaim" : 50000.0
            }
        }
    

    如果您只发布以下内容,则应避免该问题,因为它实际上是一组收入对象:

    [
        {
            "_id" : 271234.0, 
            "type" : "salary", 
            "amount" : 100000.0, 
            "inception" : "11/8/1986", 
            "endsOn" : "11/8/2030", 
            "salary" : {
                "ctc" : 200000.0, 
                "basic" : 32000.0, 
                "pf" : 14000.0, 
                "gratuity" : 55000.0, 
                "paci" : 5000.0, 
                "sa" : 50000.0, 
                "mediclaim" : 50000.0
            }
        }
    ]
    

    您可以使用 Jackson ObjectMapper 在单元测试中对此进行测试:

    Income[] incomes = new ObjectMapper().readValue(jsonAsString,Income[].class);
    

    你可以从中获得什么:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2020-09-23
      • 2016-07-30
      • 2015-03-10
      • 2019-10-25
      • 2015-12-24
      • 2018-12-03
      • 2020-07-07
      • 2011-12-02
      • 2021-03-16
      相关资源
      最近更新 更多