1

我的测试服务器返回了一个 Json 字符串,Android 应用程序运行良好,但是当我尝试使用 MappingJackson2HttpMessageConverter 将它移动到一个对象中时,它在一个列表字段上失败。

似乎无法弄清楚为什么,尝试了许多注释但没有成功,错误是 get 是:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "HourlySalesFigures" (class org.springframework.android.showcase.DayData), 
not marked as ignorable (15 known properties: , "nextDate", "totalLoyalty", "previousDate", "totalCheque", "numberOfLoyaltySales", "totalGrand", "numberOfChequeSales", "hourlySalesFigures", "totalCard", "totalCash", "totalCharge" [truncated]])

返回的 JSON 数据:

{"HourlySalesFigures":[{"hour":0,"numberOfSales":0,"totalOfSales":0},{"hour":1,"numberOfSales":0,"totalOfSales":0},{"hour":2,"numberOfSales":0,"totalOfSales":0},{"hour":3,"numberOfSales":0,"totalOfSales":0},{"hour":4,"numberOfSales":0,"totalOfSales":0},{"hour":5,"numberOfSales":0,"totalOfSales":0},{"hour":6,"numberOfSales":0,"totalOfSales":0},{"hour":7,"numberOfSales":0,"totalOfSales":0},{"hour":8,"numberOfSales":0,"totalOfSales":0},{"hour":9,"numberOfSales":0,"totalOfSales":0},{"hour":10,"numberOfSales":4,"totalOfSales":548},{"hour":11,"numberOfSales":1,"totalOfSales":3},{"hour":12,"numberOfSales":7,"totalOfSales":209.5},{"hour":13,"numberOfSales":8,"totalOfSales":528.4},{"hour":14,"numberOfSales":13,"totalOfSales":1196.8},{"hour":15,"numberOfSales":9,"totalOfSales":1422.2},{"hour":16,"numberOfSales":12,"totalOfSales":321.5},{"hour":17,"numberOfSales":22,"totalOfSales":493.5},{"hour":18,"numberOfSales":21,"totalOfSales":740.5},{"hour":19,"numberOfSales":28,"totalOfSales":1180.5},{"hour":20,"numberOfSales":8,"totalOfSales":377.5},{"hour":21,"numberOfSales":9,"totalOfSales":859.8},{"hour":22,"numberOfSales":28,"totalOfSales":11193.8},{"hour":23,"numberOfSales":1,"totalOfSales":107}],"TargetDate":"2013/02/13","PreviousDate":"2013/02/12","NextDate":"2013/02/14","NumberOfCashSales":126,"TotalCash":8432.1,"NumberOfCardSales":23,"TotalCard":9775.9,"NumberOfChequeSales":0,"TotalCheque":0,"NumberOfLoyaltySales":0,"TotalLoyalty":0,"NumberOfChargeSales":22,"TotalCharge":974.0,"TotalGrand":19182.0}

我试图将其放入的 Java 对象是(请注意,为简洁起见,我省略了 getter 和 setter)

DayData 类

@Root
public class DayData {

@ElementList(name="HourlySalesFigures", inline=true)
protected List<HourSalesItem> hourlySalesFigures;

@Element(name="TargetDate")
protected String targetDate;
@Element(name="PreviousDate")
protected String previousDate;
@Element(name="NextDate")
protected String nextDate;
@Element(name="NumberOfCashSales")
protected int numberOfCashSales;
@Element(name="TotalCash")
protected double totalCash;
@Element(name="NumberOfCardSales")
protected int numberOfCardSales;
@Element(name="TotalCard")
protected double totalCard;
@Element(name="NumberOfChequeSales")
protected int numberOfChequeSales;
@Element(name="TotalCheque")
protected double totalCheque;
@Element(name="NumberOfLoyaltySales")
protected int numberOfLoyaltySales;
@Element(name="TotalLoyalty")
protected double totalLoyalty;
@Element(name="NumberOfChargeSales")
protected int numberOfChargeSales;
@Element(name="TotalCharge")
protected double totalCharge;
@Element(name="TotalGrand")
protected double totalGrand;

public DayData(List<HourSalesItem> HourlySalesFigures, String targetDate, String previousDate, String nextDate, int numberOfCashSales, double totalCash, int numberOfCardSales, double totalCard, int numberOfChequeSales, double totalCheque, int numberOfLoyaltySales, double totalLoyalty, int numberOfChargeSales, double totalCharge, double totalGrand) {
    this.hourlySalesFigures = HourlySalesFigures;
    this.targetDate = targetDate;
    this.previousDate = previousDate;
    this.nextDate = nextDate;
    this.numberOfCashSales = numberOfCashSales;
    this.totalCash = totalCash;
    this.numberOfCardSales = numberOfCardSales;
    this.totalCard = totalCard;
    this.numberOfChequeSales = numberOfChequeSales;
    this.totalCheque = totalCheque;
    this.numberOfLoyaltySales = numberOfLoyaltySales;
    this.totalLoyalty = totalLoyalty;
    this.numberOfChargeSales = numberOfChargeSales;
    this.totalCharge = totalCharge;
    this.totalGrand = totalGrand;
}

public DayData()
{

}
}

HourSalesItem 类

@Root
public class HourSalesItem{

@Element
private int hour;
@Element
private int numberOfSales;
@Element
private double totalOfSales;

public HourSalesItem(int hour, int numberOfSales, double totalOfSales) {
    this.hour = hour;
    this.numberOfSales = numberOfSales;
    this.totalOfSales = totalOfSales;
}

public HourSalesItem(){

}
}
4

3 回答 3

0

尝试使用 @JsonIgnore 属性忽略您在代码中未处理/解析的字段。也许它会解决这个问题。如果您有不同的 JSON 解析 API,那么您只需要搜索如何忽略 JSON 密钥?

于 2013-08-19T13:49:46.297 回答
0

{"hour":0,"numberOfSales":0,"totalOfSales":0}

JSON 文件是{"Key":"value"}一对。根据 Software 的语言,它只理解 Key 的字符串类型和 value 的字符串类型。所以尝试将其更改为

{"hour":0,"numberOfSales":"0","totalOfSales":"0"}

并在您的最后相应地解析它。

由于 JSON 格式,我将其作为第二个答案。

于 2013-08-19T14:09:44.600 回答
0

解决了

服务器 REST 返回带有大写驼峰变量的 Json,例如

TotalGrand

重命名为

totalGrand

解决了问题。

于 2013-08-20T11:35:32.507 回答