其他分享
首页 > 其他分享> > fastJson接收简单嵌套json数组对象

fastJson接收简单嵌套json数组对象

作者:互联网

json字符串

{
  "stock" : [ {
    "goodsId" : "1001",
    "name" : "苹果手机",
    "number" : 2
  }, {
    "goodsId" : "1002",
    "name" : "华为手机",
    "number" : 5
  }, {
    "goodsId" : "1003",
    "name" : "小米手机",
    "number" : 3
  }, {
    "goodsId" : "1004",
    "name" : "天音手机",
    "number" : 12
  } ],
  "order" : [ {
    "goodsId" : "1001",
    "name" : "苹果手机",
    "number" : 1
  }, {
    "goodsId" : "1002",
    "name" : "华为手机",
    "number" : 2
  }, {
    "goodsId" : "1003",
    "name" : "小米手机",
    "number" : 1
  }, {
    "goodsId" : "1004",
    "name" : "天音手机",
    "number" : 3
  }, {
    "goodsId" : "1002",
    "name" : "华为手机",
    "number" : 1
  } ]
}

解决办法:

 public void countById(@RequestBody  String strJson) throws JsonProcessingException {

        JSONObject jsonObject = JSON.parseObject(strJson);
        System.out.println(jsonObject.get("stock"));
        JSONArray jsonArray = (JSONArray) jsonObject.get("stock");
        
        List<goods> list=JSONArray.parseArray(jsonArray.toString(),goods.class);
        for (goods stock : list) {
            System.out.println(stock);
        }
    }

每日一记

标签:fastJson,name,JSONArray,goodsId,number,嵌套,json,手机,stock
来源: https://blog.csdn.net/weixin_42578778/article/details/114485473