关于JSON存放List集合的顺序问题近期使⽤这个json⽣成sign签名,因为顺序问题导致⼀直验证不成功,说下解决⽅法。
使⽤的为alibaba的fastjson,版本为1.1.29
类定义
public class DetailBean implements Serializable {
private static final long serialVersionUID =1L;
private List<detail> detail;
private int years;
public class detail {
private String name;
private String serviceKey;
private BigDecimal price =new BigDecimal("0");
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getServiceKey(){
return serviceKey;
}
public void setServiceKey(String serviceKey){
this.serviceKey = serviceKey;
}
public BigDecimal getPrice(){
return price;
}
public void setPrice(BigDecimal price){
this.price = price;
}
}
public List<detail>getDetail(){
return detail;
}
public void setDetail(List<detail> detail){
this.detail = detail;
}
public int getYears(){
return years;
}
public void setYears(int years){
}
@Override
public String toString(){
return"DetailBean [detail="+ detail +", years="+ years +"]"; }
}
然后存放进JSON⾥⾯
JSONObject contentMap =new JSONObject();
contentMap.put("detail", Detail());
contentMap.put("years", Years());
DetailBean实体类的属性为
{
"detail":[{
"name":"abc",
"serviceKey":"servicepackage_base",
"price":2400
},{
"name":"def",
"serviceKey":"addedvalue_repurchase",
"price":600
}],
"years":2
}
然后输出JSON为
{"detail":[{"name":"abc","price":2400,"serviceKey":"servicepackage_base"},{"name":"def","price":600,"serviceKey":"addedvalue_repurchase"}],"years":2}但是我想要的是按照输⼊的顺序显⽰
{"detail":[{"name":"abc","serviceKey":"servicepackage_base","price":2400},{"name":"def","serviceKey":"addedvalue_repurchase","price":600}],"years":2} JSONObject的排序为字母排序,就是按照abcd…排序,按照固定排序的话,有三种⽅法:
第⼀种更改版本,升级为1.1.42以上的版本,这个版本⽀持使⽤JSONField,改变指定顺序
public class detail {
@JSONField(ordinal =1)
private String name;
@JSONField(ordinal =2)
private String serviceKey;
@JSONField(ordinal =3)
private BigDecimal price =new BigDecimal("0");
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getServiceKey(){
return serviceKey;
}
public void setServiceKey(String serviceKey){
this.serviceKey = serviceKey;
}
public BigDecimal getPrice(){
return price;
}
public void setPrice(BigDecimal price){
this.price = price;
}
}
第⼆种: 使⽤低版本的情况下,使⽤JSONType也可以达到要求
@JSONType(orders ={"name","serviceKey","price"})
public class detail {
private String name;
private String serviceKey;
private BigDecimal price =new BigDecimal("0");
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getServiceKey(){
return serviceKey;
}
public void setServiceKey(String serviceKey){
this.serviceKey = serviceKey;
}
public BigDecimal getPrice(){
return price;
}
public void setPrice(BigDecimal price){
this.price = price;
}fastjson忽略属性
}
第三种更改属性名字,使⽤低版本,但同时,使⽤这种⽅法,就只能使⽤更改的名字获取值。
public class detail {
@JSONField(name="a")
private String name;
@JSONField(name="b")
private String serviceKey;
@JSONField(name="c")
private BigDecimal price =new BigDecimal("0");
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getServiceKey(){
return serviceKey;
}
public void setServiceKey(String serviceKey){ this.serviceKey = serviceKey;
}
public BigDecimal getPrice(){
return price;
}
public void setPrice(BigDecimal price){
this.price = price;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论