//data是数组[{"goodsId":"3","num":"1"},{"goodsId":"2","num":"1"}] JSONArray arrar = JSONArray.parseArray(data); for(int j=0;j<arrar.size();j++){ System.out.println(arrar.get(j).toString()); OrderInfo orderInfo = new OrderInfo(); JSONObject item = JSONObject.parseObject(arrar.get(j).toString()); Goods goods = goodsDao.findId(Integer.parseInt(item.getString("goodsId")));
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency>
<!-- 官方推荐使用fastjson2--> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.27</version> </dependency>
JSONObject result = new JSONObject(); JSONArray array = JSONArray.parseArray(json); System.out.println(array.size()); List<GoodsPrice> goodsPriceList = new ArrayList<GoodsPrice>(); for(int i=0;i<array.size();i++) { JSONObject item = (JSONObject) array.get(i); //System.out.println(item); Date date = item.getDate("date"); BigDecimal price = item.getBigDecimal("price"); Integer taocanId = item.getInteger("taocanId"); if (item.containsKey("id")) { System.out.println("有id" ); }else { System.out.println("没有id" ); } GoodsPrice goodsPrice = new GoodsPrice(); goodsPrice.setGoodsId(goodsId); goodsPrice.setGoodsTaoCanId(taocanId); goodsPrice.setPrice(price); goodsPrice.setPriceDate(date); goodsPriceList.add(goodsPrice); }
站长微信:xiaomao0055
站长QQ:14496453