用Fastjson的朋友帮忙看个问题,是否是小弟没找到里头的玄机

1、实体类及属性如下
public class StockInfo implements Serializable {
private String kPrice;
private String sPrice;
private String hPrice;
private String lPrice;
private String cPrice;
private String change;
private String changePercent;
private String stockTime;
...}

2、用阿里巴巴的fastjson
JSON.toJSONString(实体类),只有change、changePercent、stockTime被序列化,其它属性都被丢弃,帮忙看看是什么问题?

使用fastjson:{"change":"0.089","changePercent":"3.722","stockTime":"2014/05/26 16:00:27"}

使用其它JSON工具:{"cPrice":"2.480","change":"0.089","changePercent":"3.722","hPrice":"2.500","kPrice":"2.390","lPrice":"2.390","sPrice":"2.391","stockTime":"2014/05/26 16:00:27"}

0

2个回答

你的代码不合javabean的规范,也就是你的getter和setter方法不合规范。应该是

public String getKPrice()

而不是 public String getkPrice()

fastJSON是根据getter方法来序列化的。

完整的代码如下:

[code="java"]public class StockInfo implements Serializable {

private String kPrice;
private String sPrice;
private String hPrice;
private String lPrice;
private String cPrice;
private String change;
private String changePercent;
private String stockTime;

public String getKPrice() {
    return kPrice;
}

public void setKPrice(String kPrice) {
    this.kPrice = kPrice;
}

public String getSPrice() {
    return sPrice;
}

public void setSPrice(String sPrice) {
    this.sPrice = sPrice;
}

public String getHPrice() {
    return hPrice;
}

public void setHPrice(String hPrice) {
    this.hPrice = hPrice;
}

public String getLPrice() {
    return lPrice;
}

public void setLPrice(String lPrice) {
    this.lPrice = lPrice;
}

public String getCPrice() {
    return cPrice;
}

public void setCPrice(String cPrice) {
    this.cPrice = cPrice;
}

public String getChange() {
    return change;
}

public void setChange(String change) {
    this.change = change;
}

public String getChangePercent() {
    return changePercent;
}

public void setChangePercent(String changePercent) {
    this.changePercent = changePercent;
}

public String getStockTime() {
    return stockTime;
}

public void setStockTime(String stockTime) {
    this.stockTime = stockTime;
}

}[/code]

0
weixin_42509558
weixin_42509558 对对,我那个实体类是用eclipse生成的,没有过多的检查,以后不能太依赖它了
大约 5 年之前 回复

还有一个就是,属性为null也会不丢弃。

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!