Java 把数据转换成需要的数据格式

后台查到的数据格式比较复杂,

{
  "id": "1_1",
  "name": "test1",
  "companyCode": null,
  "company": null,
  "distributions": [
      {
          "id": 3,
          "shopTag": {
              "id": "100",
              "name": "test1",
              "remark": "test1",
              "priority": 0,
              "shopDimensionId": "1",
              "createdAt": null,
              "updatedAt": null,
              "operation": null
          },
          "goodsTag": {
              "id": "1",
              "name": "test2",
              "remark": "test2",
              "priority": 0,
              "goodsDimensionId": "1",
              "shopTagId": "100",
              "createdAt": null,
              "updatedAt": null,
              "operation": null
          },
          "distributionItems": [
              {
                  "id": 3,
                  "size": "l",
                  "sizeGroup": "C",
                  "stdQty": 1,
                  "minQty": 1,
                  "maxQty": 1
              },
              {
                  "id": 4,
                  "size": "x",
                  "sizeGroup": "B",
                  "stdQty": 1,
                  "minQty": 1,
                  "maxQty": 1
              }
          ],
          "createdAt": "2020-04-12T09:09:37.051+0000",
          "operation": null
      }
  ]
}

前端需要的数据格式:

{
  "shopTags": [],
  "goodsTags": [],
  "distribution": {
    "[sizeGroupX]": [
      {
        "[shopTagIdX]": {
          "goodsTagId": "123",
          "maxQtyTotal": 1,
          "minQtyTotal": 1,
          "stdQtyTotal": 1
        },
        "goodsTag": {}
      }
    ]
  }
}

直接传给前端,前端页面性能受影响,
想在java里面处理成前端需要的数据格式,如何使用stream流来实现?

阅读 1.6k
1 个回答

把数据用jsonObject存起来,然后根据你的需要遍历这个object,生成你需要的数据。我最近刚做了一个遍历jsonArray的你可以参考一下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题