最近搜索

alibaba.fastjson 解析 json字符串

浏览:999
管理员 2020-02-18 22:45



//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>



判断 有没有  某个值  

判断key是否存在


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