欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

将redis中的json转换成对象

程序员文章站 2022-05-24 10:57:02
...

JSONArray parse = (JSONArray) JSONObject.parse(ogj);
List seckills = parse.toJavaList(Seckill.class);
将json转成list对象

JSONObject.toJavaObject(JSONObject.parseObject(ogj),Seckill.class);
json转换成对象

实体类要和redis中的json相互对应
package com.spring.test.dm;


import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableName;

import java.io.Serializable;
import java.util.Date;

@TableName("seckill")
public class Seckill  extends  Model<Seckill> {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private Integer number;
    @TableField("start_time")
    private Date startTime;
    @TableField("end_time")
    private Date endTime;
    @TableField("create_time")
    private Date createTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public  String  toString(){
        return "Seckill{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", number=" + number +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", createTime=" + createTime +
                '}';
    }
    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}

 @Override
    public List<SeckillRes> getSeckillList(SeckillReq req) {
        Object obj = jedisClient.get("seckill");
        List<SeckillRes> seckillRes =new List<SeckillRes>() {
            @Override
            public int size() {
                return 0;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public boolean contains(Object o) {
                return false;
            }

            @Override
            public Iterator<SeckillRes> iterator() {
                return null;
            }

            @Override
            public Object[] toArray() {
                return new Object[0];
            }

            @Override
            public <T> T[] toArray(T[] a) {
                return null;
            }

            @Override
            public boolean add(SeckillRes seckillRes) {
                return false;
            }

            @Override
            public boolean remove(Object o) {
                return false;
            }

            @Override
            public boolean containsAll(Collection<?> c) {
                return false;
            }

            @Override
            public boolean addAll(Collection<? extends SeckillRes> c) {
                return false;
            }

            @Override
            public boolean addAll(int index, Collection<? extends SeckillRes> c) {
                return false;
            }

            @Override
            public boolean removeAll(Collection<?> c) {
                return false;
            }

            @Override
            public boolean retainAll(Collection<?> c) {
                return false;
            }

            @Override
            public void clear() {

            }

            @Override
            public SeckillRes get(int index) {
                return null;
            }

            @Override
            public SeckillRes set(int index, SeckillRes element) {
                return null;
            }

            @Override
            public void add(int index, SeckillRes element) {

            }

            @Override
            public SeckillRes remove(int index) {
                return null;
            }

            @Override
            public int indexOf(Object o) {
                return 0;
            }

            @Override
            public int lastIndexOf(Object o) {
                return 0;
            }

            @Override
            public ListIterator<SeckillRes> listIterator() {
                return null;
            }

            @Override
            public ListIterator<SeckillRes> listIterator(int index) {
                return null;
            }

            @Override
            public List<SeckillRes> subList(int fromIndex, int toIndex) {
                return null;
            }
        };

        if (obj==null) {
            EntityWrapper<Seckill> ew = new EntityWrapper<Seckill>();
            /*ew.eq("name","1000元秒杀iphone6");*/
            List<Seckill> a = seckillMapper.selectList(ew);
            String ogj=    jedisClient.set("seckill",JSON.toJSONString(a));
            jedisClient.expire("seckill",100);
            JSONArray parse = (JSONArray) JSONObject.parse(ogj);
            List<Seckill> seckills = parse.toJavaList(Seckill.class);
//            Seckill seckill =     JSONObject.toJavaObject(JSONObject.parseObject(ogj),Seckill.class);
            List<SeckillRes> b = zDozerBeanUtil.convertList(seckills, SeckillRes.class);
            return  b;
        }else {
           String json =   jedisClient.get("seckill");
            JSONArray parse = (JSONArray) JSONObject.parse(json);
            List<Seckill> seckills = parse.toJavaList(Seckill.class);
            List<SeckillRes> b = zDozerBeanUtil.convertList(seckills, SeckillRes.class);
            return  b;
        }
    }