我在使用fastJSON 序列化一个 带有 Map<Integer,Order>类型的属性的对象的 的时候 发现正确序列化的json 字符串
格式错误
fastJSON 版本: 1.2.54
格式错误如下:
很明显看到红色部分 fastjson 将数字作为了 json的键 而没有加上双引号 导致前端解析json错误
{
"Status": "购物车不为空",
"data": {
2: {
"count": 2,
"product": {
"integral": 5,
"monthlySale": 155,
"picture01": "./Product/JavaThink01.jpg",
"picture2": "./Product/JavaThing02.jpg",
"picture3": "./Product/JavaThing03\t.jpg",
"picture4": "./Product/JavaThing04.jpg",
"picture5": "./Product/JavaThing05.jpg",
"price": 86.4,
"productID": 2,
"productName": "Java编程思想第4版/第四版/thinking in java中文版/java基础入门书",
"review": 4171,
"saveCount": 45,
"typeID": 1
}
},
6: {
"count": 2,
"product": {
"integral": 10,
"monthlySale": 67,
"picture01": "./Product/go01.jpg",
"picture2": "./Product/go02.jpg",
"picture3": "./Product/go03.jpg",
"picture4": "./Product/go04.jpg",
"picture5": "./Product/go05.jpg",
"price": 56.8,
"productID": 6,
"productName": "Go语言核心编程 Go语言编程入门教程书籍 golang教程实战",
"review": 206,
"saveCount": 120,
"typeID": 1
}
}
},
"HasProduct": true,
"meonyAll": 286.4,
"Count": 4
}
Java代码为:
JSONObject obj = new JSONObject();
if(request.getSession().getAttribute("Cars") == null){
//购物车没有物品 为空
obj.put("HasProduct",false);
obj.put("Status","购物车为空");
}else{
Map<Integer,Order> shoppingCar =(Map<Integer,Order>)request.getSession().getAttribute("Cars");
if(shoppingCar.keySet().isEmpty()){
obj.put("Status","购物车为空");
obj.put("HasProduct",false);
}else{
request.getSession().setMaxInactiveInterval(60*60);
obj.put("Status","购物车不为空");
obj.put("HasProduct",true);
obj.put("data",shoppingCar);
int count = 0;
float meonyAll = 0.0f;
//商品件数
for (Integer val :shoppingCar.keySet()) {
Order order = (Order)shoppingCar.get(val);
count += order.getCount();
meonyAll += (order.getCount() * order.getProduct().getPrice());
}
obj.put("Count",count);
obj.put("meonyAll",meonyAll);
}
}
Order 类
public class Order {
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Order() {
}
public Order(Product product, int count) {
this.product = product;
Count = count;
}
public int getCount() {
return Count;
}
public void setCount(int count) {
Count = count;
}
private int Count;
}
product 类
public class Product {
public int productID;
public int typeID;
public String productName;
public float price;
public int monthlySale;
public int review;
public int saveCount;
public int integral;
public String picture01;
public String picture2;
public String picture3;
public String picture4;
public String picture5;
public Product(int productID, int typeID, String productName, float price, int monthlySale, int review, int saveCount, int integral, String picture01, String picture2, String picture3, String picture4, String picture5) {
this.productID = productID;
this.typeID = typeID;
this.productName = productName;
this.price = price;
this.monthlySale = monthlySale;
this.review = review;
this.saveCount = saveCount;
this.integral = integral;
this.picture01 = picture01;
this.picture2 = picture2;
this.picture3 = picture3;
this.picture4 = picture4;
this.picture5 = picture5;
}
public Product(){
}
public int getProductID() {
return productID;
}
public void setProductID(int productID) {
this.productID = productID;
}
public int getTypeID() {
return typeID;
}
public void setTypeID(int typeID) {
this.typeID = typeID;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getMonthlySale() {
return monthlySale;
}
public void setMonthlySale(int monthlySale) {
this.monthlySale = monthlySale;
}
public int getReview() {
return review;
}
public void setReview(int review) {
this.review = review;
}
public int getSaveCount() {
return saveCount;
}
public void setSaveCount(int saveCount) {
this.saveCount = saveCount;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public String getPicture01() {
return picture01;
}
public void setPicture01(String picture01) {
this.picture01 = picture01;
}
public String getPicture2() {
return picture2;
}
public void setPicture2(String picture2) {
this.picture2 = picture2;
}
public String getPicture3() {
return picture3;
}
public void setPicture3(String picture3) {
this.picture3 = picture3;
}
public String getPicture4() {
return picture4;
}
public void setPicture4(String picture4) {
this.picture4 = picture4;
}
public String getPicture5() {
return picture5;
}
public void setPicture5(String picture5) {
this.picture5 = picture5;
}
}
3条答案
按热度按时间fquxozlt1#
使用WriteNonStringKeyAsString试试看
xzlaal3s2#
可以了 谢谢啦 👍
doinxwow3#
建议把此选项作为默认选项吧 @wenshao