FastJson解析错误:com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze
程序员文章站
2022-03-23 12:30:12
...
1.错误代码
/***
* 获取商品秒杀政策
* @param list
* @return
*/
private Map<String, Object> getLimitPolicy(ArrayList<Map<String, Object>> list){
Map<String, Object> resultMap = new HashMap<String, Object>();
for (Map<String, Object> skuMap: list){
//3.1、从redis取出政策
String policy = stringRedisTemplate.opsForValue().get("LIMIT_POLICY_"+skuMap.get("sku_id").toString());
//3.2、判断有政策的才继续
if (policy!=null&&!policy.equals("")){
// Map<String, Object> policyInfo = JSONObject.parseObject(policy, Map.class);
Limit_Policy limit_policy=JSONObject.parseObject(policy,Limit_Policy.class);
//3.3、开始时间小于等于当前时间,并且当前时间小于等于结束时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String now = restTemplate.getForObject("http://leyou-time-server/getTime", String.class);
try {
Date end_time = simpleDateFormat.parse(limit_policy.getEnd_time().toString());
Date begin_time = simpleDateFormat.parse(limit_policy.getBegin_time().toString());
Date now_time = simpleDateFormat.parse(now);
if (begin_time.getTime()<=now_time.getTime()&&now_time.getTime()<=end_time.getTime()){
skuMap.put("limitPrice", limit_policy.getPrice());
skuMap.put("limitQuanty", limit_policy.getQuanty());
skuMap.put("limitBeginTime", limit_policy.getBegin_time());
skuMap.put("limitEndTime", limit_policy.getEnd_time());
skuMap.put("nowTime", now);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
resultMap.put("result", true);
resultMap.put("msg", "");
return resultMap;
}
出错位置: Limit_Policy limit_policy=JSONObject.parseObject(policy,Limit_Policy.class);
2.redis 数据
Limit_Policy{id=0, sku_id=26816294479, quanty=7777, price=1000, begin_time=Sun Dec 29 19:00:00 CST 2019, end_time=Mon Dec 30 06:00:00 CST 2019}
3.实体类
package com.zengjx.leyou.pojo;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Date;
/**
* @ClassName HelloController
* @Description TODO
* @Author zengjx
* @Company zengjx
* @Date 2019/12/29 16:30
* @Version V1.0
*/
@Component
public class Limit_Policy implements Serializable {
private long id;
private long sku_id;
private int quanty;
private int price;
private Date begin_time;
private Date end_time;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getSku_id() {
return sku_id;
}
public void setSku_id(long sku_id) {
this.sku_id = sku_id;
}
public int getQuanty() {
return quanty;
}
public void setQuanty(int quanty) {
this.quanty = quanty;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Date getBegin_time() {
return begin_time;
}
public void setBegin_time(Date begin_time) {
this.begin_time = begin_time;
}
public Date getEnd_time() {
return end_time;
}
public void setEnd_time(Date end_time) {
this.end_time = end_time;
}
public Limit_Policy() {
}
@Override
public String toString() {
return "Limit_Policy{" +
"id=" + id +
", sku_id=" + sku_id +
", quanty=" + quanty +
", price=" + price +
", begin_time=" + begin_time +
", end_time=" + end_time +
'}';
}
}
4.错误日志
com.alibaba.fastjson.JSONException: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.58
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:489) ~[fastjson-1.2.58.jar:na]
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1537) ~[fastjson-1.2.58.jar:na]
at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_Limit_Policy.deserialze(Unknown Source) ~[na:na]
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:284) ~[fastjson-1.2.58.jar:na]
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:689) ~[fastjson-1.2.58.jar:na]
at com.alibaba.fastjson.JSON.parseObject(JSON.java:378) ~[fastjson-1.2.58.jar:na]
at com.alibaba.fastjson.JSON.parseObject(JSON.java:282) ~[fastjson-1.2.58.jar:na]
at com.alibaba.fastjson.JSON.parseObject(JSON.java:555) ~[fastjson-1.2.58.jar:na]
at com.zengjx.leyou.service.StockService.getLimitPolicy(StockService.java:170) ~[classes/:na]
at com.zengjx.leyou.service.StockService.getStockListBySpu_id(StockService.java:57) ~[classes/:na]
5.测试:
{
"sku_id": 26816294479,
"quanty": 7777,
"price": 1000,
"begin_time": "2019-12-29T11:00:00.000+0000",
"end_time": "2019-12-29T22:00:00.000+0000"
}
public static void main(String[] args) {
String policy="{\n" +
"\t\n" +
" \"sku_id\": 26816294479,\n" +
" \"quanty\": 7777,\n" +
" \"price\": 1000,\n" +
" \"begin_time\": \"2019-12-29T11:00:00.000+0000\",\n" +
" \"end_time\": \"2019-12-29T22:00:00.000+0000\"\n" +
"}";
Limit_Policy parseObject = JSONObject.parseObject(policy, Limit_Policy.class);
System.out.println(parseObject);
}
出错的json数据:与上面不同的是以LimitPolicy开头字符串
使用json格式在线校验下面这行redis 读取的数据。
Limit_Policy {
id = 0, sku_id = 26816294479, quanty = 7777, price = 1000, begin_time = Sun Dec 29 19: 00: 00 CST 2019, end_time = Mon Dec 30 06: 00: 00 CST 2019
}
校验出错:在线校验 http://www.bejson.com/
原因是实体类:重写了toString
//存入redis
stringRedisTemplate.opsForValue().set("LIMIT_POLICY_"+limit_policy.getSku_id(),limit_policy.toString(),end_diff, TimeUnit.SECONDS);
以下测试可以相互转换
FastJson对实体类和Json还有JSONObject相互转换
1. 实体类或集合转JSON串
String besnString = JSONObject.toJSONString(实体类);
2.JSON串转JSONObject
JSONObject jsonObject = JSONObject.parseObject(jsonString);
3.JSON串转实体类
实体类 javaBean = JSON.parseObject(json, 实体类.class);
4.JSON串转带泛型的List的集合
List<实体类或其他泛型> list = JSON.parseObject(json, new TypeReference<List<实体类或其他泛型>>(){});
public static void main(String[] args) {
test2();
}
public static void test1(){
String policy="{\n" +
"\t\n" +
" \"sku_id\": 26816294479,\n" +
" \"quanty\": 7777,\n" +
" \"price\": 1000,\n" +
" \"begin_time\": \"2019-12-29T11:00:00.000+0000\",\n" +
" \"end_time\": \"2019-12-29T22:00:00.000+0000\"\n" +
"}";
Limit_Policy parseObject = JSONObject.parseObject(policy, Limit_Policy.class);
System.out.println(parseObject);
}
public static void test2(){
Limit_Policy limit_policy =new Limit_Policy();
limit_policy.setBegin_time(new Date());
limit_policy.setEnd_time(new Date());
limit_policy.setId(11111);
limit_policy.setPrice(11111333);
String besnString = JSONObject.toJSONString(limit_policy);
System.out.println(besnString);//{"begin_time":1577625875722,"end_time":1577625875722,"id":11111,"price":11111333,"quanty":0,"sku_id":0}
Limit_Policy parseObject = JSONObject.parseObject(besnString, Limit_Policy.class);
System.out.println("反转"+parseObject);
}
6.修改
原来: stringRedisTemplate.opsForValue().set("LIMIT_POLICY_"+limit_policy.getSku_id(),limit_policy.toString(),end_diff, TimeUnit.SECONDS);
修改后
String besnString = JSONObject.toJSONString(limit_policy);
stringRedisTemplate.opsForValue().set("LIMIT_POLICY_"+limit_policy.getSku_id(),besnString,end_diff, TimeUnit.SECONDS);
//存入redis
String besnString = JSONObject.toJSONString(limit_policy);
// stringRedisTemplate.opsForValue().set("LIMIT_POLICY_"+limit_policy.getSku_id(),limit_policy.toString(),end_diff, TimeUnit.SECONDS);
stringRedisTemplate.opsForValue().set("LIMIT_POLICY_"+limit_policy.getSku_id(),besnString,end_diff, TimeUnit.SECONDS);