Jackson解析:无法识别的标记'tieheT':期待'null','true'或'false'

时间:2012-11-26 13:09:11

标签: jquery jackson

我遇到了通过jackson解析器,ajax和jquery将对象数组传递给控制器​​的问题。

我的控制器看起来像这样:

@RequestMapping(value="/saveTimeBooking")
public @ResponseBody ReturnTO saveTimeBooking(@RequestBody TimesheetTO timesheetTO[]) {
    // ...
}

My Java Object TimesheetTO如下所示:

public class TimesheetTO implements Serializable {
    private static final long serialVersionUID = 1L;

    private int activityMasterId;
    private String name;

    private BigDecimal mondayEffort=new BigDecimal(0);
    private BigDecimal tuesdayEffort=new BigDecimal(0);
    private BigDecimal wednesdayEffort=new BigDecimal(0);
    private BigDecimal thursdayEffort=new BigDecimal(0);
    private BigDecimal fridayEffort=new BigDecimal(0);
    private BigDecimal saturdayEffort=new BigDecimal(0);
    private BigDecimal sundayEffort=new BigDecimal(0);
    public int getActivityMasterId() {
        return activityMasterId;
    }
    public String getName() {
        return name;
    }
    public BigDecimal getMondayEffort() {
        return mondayEffort;
    }
    public BigDecimal getTuesdayEffort() {
        return tuesdayEffort;
    }
    public BigDecimal getWednesdayEffort() {
        return wednesdayEffort;
    }
    public BigDecimal getThursdayEffort() {
        return thursdayEffort;
    }
    public BigDecimal getFridayEffort() {
        return fridayEffort;
    }
    public BigDecimal getSaturdayEffort() {
        return saturdayEffort;
    }
    public BigDecimal getSundayEffort() {
        return sundayEffort;
    }
    public void setActivityMasterId(int activityMasterId) {
        this.activityMasterId = activityMasterId;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setMondayEffort(BigDecimal mondayEffort) {
        this.mondayEffort = mondayEffort;
    }
    public void setTuesdayEffort(BigDecimal tuesdayEffort) {
        this.tuesdayEffort = tuesdayEffort;
    }
    public void setWednesdayEffort(BigDecimal wednesdayEffort) {
        this.wednesdayEffort = wednesdayEffort;
    }
    public void setThursdayEffort(BigDecimal thursdayEffort) {
        this.thursdayEffort = thursdayEffort;
    }
    public void setFridayEffort(BigDecimal fridayEffort) {
        this.fridayEffort = fridayEffort;
    }
    public void setSaturdayEffort(BigDecimal saturdayEffort) {
        this.saturdayEffort = saturdayEffort;
    }
    public void setSundayEffort(BigDecimal sundayEffort) {
        this.sundayEffort = sundayEffort;
    }

    public TimesheetTO(){}

    public TimesheetTO( String activityMasterId, 
                        String name, 
                        String mondayEffort,
                        String tuesdayEffort,
                        String wednesdayEffort,
                        String thursdayEffort,
                        String fridayEffort,
                        String saturdayEffort,
                        String sundayEffort)
    {
        this.activityMasterId=Integer.parseInt(activityMasterId);
        this.name=name;
        this.mondayEffort=BigDecimal.valueOf(Double.parseDouble(mondayEffort));
        this.tuesdayEffort=BigDecimal.valueOf(Double.parseDouble(tuesdayEffort));
        this.wednesdayEffort=BigDecimal.valueOf(Double.parseDouble(wednesdayEffort));
        this.thursdayEffort=BigDecimal.valueOf(Double.parseDouble(thursdayEffort));
        this.fridayEffort=BigDecimal.valueOf(Double.parseDouble(fridayEffort));
        this.saturdayEffort=BigDecimal.valueOf(Double.parseDouble(saturdayEffort));
        this.sundayEffort=BigDecimal.valueOf(Double.parseDouble(sundayEffort));
    }
}

并且,我使用以下jquery代码将时间表对象数组传递给控制器​​:

var datacounter=0;
/*var totalcount=0;*/
var timesheetTO = new Array();

$("#timeSheetTableId input[id^=activityMasterId]").each(function() {
       $('#activityMasterId_'+datacounter).removeAttr('disabled');
       $('#name_'+datacounter).removeAttr('disabled');

       timesheetTO.push({
           "activityMasterId": $('#activityMasterId_'+datacounter).val(),
           "name": $('#name_'+datacounter).val(),
           "mondayEffort": new Integer($('#mondayEffort_'+datacounter).val()),
           "tuesdayEffort": $('#tuesdayEffort_'+datacounter).val(),
           "wednesdayEffort": $('#wednesdayEffort_'+datacounter).val(),
           "thursdayEffort": $('#thursdayEffort_'+datacounter).val(),
           "fridayEffort": $('#fridayEffort_'+datacounter).val(),
           "saturdayEffort": $('#saturdayEffort_'+datacounter).val(),
           "sundayEffort": $('#sundayEffort_'+datacounter).val()
       });
       datacounter=datacounter+1;
});

var url = contextRoot + "timesheet/saveTimeBooking.htm";
$.ajax({
    type : 'POST',
    url : url,
    async : false,
    timeout : 5000,
    contentType: 'application/json',
    dataType: 'json',
    data : {
        timesheetTO : JSON.stringify(timesheetTO)
    },
    success : function(data, textStatus) {
        alert('successful');

    },
    error : function(xhr, textStatus, errorThrown) {
        alert('request failed in saving timesheet:' + errorThrown + " " + textStatus + " "
                + xhr.toString());
    }
});

提交时,我收到上面给出的错误:Jacskon游标异常:无法识别的标记'tieheT':期待'null','true'或'false'

我做错了什么..有什么方法可以解决这个问题吗?

以下是JSON.stringify的结果:

 [{"activityMasterId":"1","name":"Financial Implications","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thur‌​sdayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"},{"act‌​ivityMasterId":"2","name":"Cost estimation","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thursd‌​ayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"}] 

5 个答案:

答案 0 :(得分:3)

您需要验证您是否正在提供有效的JSON:错误消息表明情况并非如此。它还应该为您提供准确的行号和行号,您可以使用它来查找输入JSON的问题。从消息中,我猜测没有引用任何属性名称,或者没有引用某些字符串值。

答案 1 :(得分:2)

我遇到了类似的问题,现在工作正常。 问题可能是

data : {
        timesheetTO : JSON.stringify(timesheetTO)
    }

将其括在双引号

data : {
        ' "timesheetTO" : " '+JSON.stringify(timesheetTO)+' " '
    }

答案 2 :(得分:2)

问题是您传递的字符串值缺少引号,因此编译器期望使用布尔/空对象。

您应该验证运行时通过调试传递的值,方法是将其复制到文本编辑器中,以便在必要时进行全面分析。

答案 3 :(得分:0)

ajax数据应该采用JSON对象的字符串。请参阅此处的解决方案:

Using jQuery to post JSON object to Spring 3 controller

答案 4 :(得分:0)

或者,您可以用@RestController注释您的班级(如果尚未添加)。 然后,您无需使用@RequestBody@ResponseBody

显式注释您的请求和响应

因此您的方法签名将更改为

@PostMapping("/saveTimeBooking")
public ReturnTO saveTimeBooking(TimesheetTO timesheetTO[]) {
// ...
}