使用JSON将购物车中的对象存到Cookie中

来源:互联网 发布:win10自带看图软件 编辑:程序博客网 时间:2024/06/04 18:20
public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        response.setCharacterEncoding("utf-8");        response.setContentType("text/html;charset=UTF-8");        PrintWriter out = response.getWriter();        //取出数据        String bookId = request.getParameter("bookId");        String bookName = request.getParameter("bookName");        String bookPrice = request.getParameter("bookPrice");        String bookPriceOld = request.getParameter("bookPriceOld");        //1.从cookie中取出json对象        Cookie[] cookies = request.getCookies();        String jsonStr = null;        if (cookies != null) {            for (Cookie c : cookies) {                if (c.getName().equals("shopCart")) {                    jsonStr = URLDecoder.decode(c.getValue(),"utf-8");                }            }        }        ArrayList<BookInfo> shopArray = new ArrayList<BookInfo>();        //2.判断jsonStr是否为空        if (jsonStr == null) {            //2.1若为空,表示购物车中没有任何东西 可直接加入            //构造图书对象            BookInfo book = new BookInfo();            book.setBookId(Integer.parseInt(bookId));            book.setBookName(bookName);            book.setBookPrice(Double.parseDouble(bookPrice));            book.setBookPriceOld(Double.parseDouble(bookPriceOld));            book.setNum(1);            //将数据存入集合ArrayList中            shopArray.add(book);            //将集合数据存入json中        } else {            //2.2不为空,表示有数据,解析出来后再判断将某一个book对象的num加1            //json字符串为 jsonStr            JSONObject jb = JSONObject.fromObject(jsonStr);            JSONArray ja = jb.getJSONArray("shopArray");            boolean isExist = false;            for (int i = 0; i < ja.size(); i++) {                BookInfo b = new BookInfo();                int bId = ja.getJSONObject(i).getInt("bookId");                int num = ja.getJSONObject(i).getInt("num");                b.setBookId(bId);                b.setBookName(ja.getJSONObject(i).getString("bookName"));                b.setBookName(ja.getJSONObject(i).getString("bookName"));                b.setBookPrice(ja.getJSONObject(i).getDouble("bookPrice"));                b.setBookPriceOld(ja.getJSONObject(i).getDouble(                        "bookPriceOld"));                if (bId == Integer.parseInt(bookId)) {                    isExist = true;                    num++;                }                b.setNum(num);                shopArray.add(b);            }            if (!isExist) {                BookInfo book = new BookInfo();                book.setBookId(Integer.parseInt(bookId));                book.setBookName(bookName);                book.setBookPrice(Double.parseDouble(bookPrice));                book.setBookPriceOld(Double.parseDouble(bookPriceOld));                book.setNum(1);                shopArray.add(book);            }        }        //将ArrayList中的数据转换成json        JSONObject jsonObj = new JSONObject();        JSONArray jsonArray = JSONArray.fromObject(shopArray);        jsonObj.put("shopArray", jsonArray);        String jsonDataStr = jsonObj.toString();        //存入cookie        Cookie shopCartCookie = new Cookie("shopCart", URLEncoder.encode(jsonDataStr, "utf-8"));        shopCartCookie.setMaxAge(60 * 60 * 24 * 7);        response.addCookie(shopCartCookie);        response.sendRedirect("cart.jsp");    }

json解析数据相关jar包—–>在这里下载

2 0