如何将两个JSON响应合并为一个结构中的一个

发布于 2025-02-13 16:51:46 字数 1760 浏览 0 评论 0原文

此响应来自一个API调用。 JSON响应1:

{
"message": "success",
"data": [
    {
        "storeid": "91",
        "productid": "37",
        "cartitemid": 48,
        "product_quantity": 8
    },
    {
        "storeid": "86",
        "productid": "74",
        "cartitemid": 52,
        "product_quantity": 1
    },
    {
        "storeid": "86",
        "productid": "73",
        "cartitemid": 51,
        "product_quantity": 6
    },
    {
        "storeid": "86",
        "productid": "76",
        "cartitemid": 50,
        "product_quantity": 1
    }
]
}

这是第二个API调用。

JSON响应2:

{
"data": [
    {
        "storeid": 91,
        "productid": 37,
        "product_name": "hhh"
    },
    {
        "storeid": 86,
        "productid": 73,
        "product_name": "dfsdfsd"
    },
    {
        "storeid": 86,
        "productid": 76,
        "product_name": "dfsdfsd"
    }
]
}

我需要此输出,因为我需要将此JSON解析到我的模型中。

和输出:

{
"storeInfo": [
    {
        "storeid": 91,
        "products": [
            {
                "productid": 37,
                "product_name": "hhh",
                "prod_images": "https://sdfsd.com"
            }
        ]
    },
    {
        "storeid": 86,
        "products": [
            {
                "productid": 73,
                "product_name": "ghgjhhj",
                "prod_images": "https://hjhjh.com"
            },
            {
                "productid": 76,
                "product_name": "reer",
                "prod_images": "https://hjhjh.com"
            }
        ]
    }
]
}

请建议如何以这种格式制作输出JSON? 我已经尝试了多种方法来合并两个JSON并将其输出以这种方式。什么都没有解决。这是否会发生在后背上还是在我们的前端手中?

This response is from one API call.
JSON Response 1:

{
"message": "success",
"data": [
    {
        "storeid": "91",
        "productid": "37",
        "cartitemid": 48,
        "product_quantity": 8
    },
    {
        "storeid": "86",
        "productid": "74",
        "cartitemid": 52,
        "product_quantity": 1
    },
    {
        "storeid": "86",
        "productid": "73",
        "cartitemid": 51,
        "product_quantity": 6
    },
    {
        "storeid": "86",
        "productid": "76",
        "cartitemid": 50,
        "product_quantity": 1
    }
]
}

This is 2nd API call.

JSON Response 2:

{
"data": [
    {
        "storeid": 91,
        "productid": 37,
        "product_name": "hhh"
    },
    {
        "storeid": 86,
        "productid": 73,
        "product_name": "dfsdfsd"
    },
    {
        "storeid": 86,
        "productid": 76,
        "product_name": "dfsdfsd"
    }
]
}

I need this output because I need to parse this json into my model.

And OUTPUT:

{
"storeInfo": [
    {
        "storeid": 91,
        "products": [
            {
                "productid": 37,
                "product_name": "hhh",
                "prod_images": "https://sdfsd.com"
            }
        ]
    },
    {
        "storeid": 86,
        "products": [
            {
                "productid": 73,
                "product_name": "ghgjhhj",
                "prod_images": "https://hjhjh.com"
            },
            {
                "productid": 76,
                "product_name": "reer",
                "prod_images": "https://hjhjh.com"
            }
        ]
    }
]
}

Please suggest how I can make the output JSON in this format?
I have tried many way to merge two json and make it output to this way. Nothing worked out. Does this suppose to be happened on backed or it is in our frontend hands?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2025-02-20 16:51:47

该输出模型必须在API输出级别上构建。

This output model must be built at the api output level.

或十年 2025-02-20 16:51:47

这确实是后端的东西。但是,如果您仍然需要它,则以下代码应在前端方面解决问题。

MERGE函数在JSON1JSON2上合并了两个产品的所有属性,然后使用提供的JSON模板格式化输出。

查看

import 'dart:convert';

final json1 = {
  "message": "success",
  "data": [
    {
      "storeid": "91",
      "productid": "37",
      "cartitemid": 48,
      "product_quantity": 8
    },
    {
      "storeid": "86",
      "productid": "74",
      "cartitemid": 52,
      "product_quantity": 1
    },
    {
      "storeid": "86",
      "productid": "73",
      "cartitemid": 51,
      "product_quantity": 6
    },
    {
      "storeid": "86",
      "productid": "76",
      "cartitemid": 50,
      "product_quantity": 1
    }
  ]
};

final json2 = {
  "data": [
    {"storeid": 91, "productid": 37, "product_name": "hhh"},
    {"storeid": 86, "productid": 73, "product_name": "dfsdfsd"},
    {"storeid": 86, "productid": 76, "product_name": "dfsdfsd"}
  ]
};

Map<String, dynamic> merge(
    Map<String, dynamic> cart, Map<String, dynamic> namedProducts) {
  final products = <String, Map<String, dynamic>>{};

  // include all cart products
  List<Map<String, dynamic>> cartData = cart['data'];
  for (final p in cartData) {
    final product = {
      ...p,
      'productid': int.parse(p['productid']),
      'storeid': int.parse(p['storeid']),
    };

    products.update(
      '${p['storeid']}:${p['productid']}',
      (value) => {
        ...value,
        ...product,
      },
      ifAbsent: () => product,
    );
  }

  // include all named products
  List<Map<String, dynamic>> namedProductsData = namedProducts['data'];
  for (final p in namedProductsData) {
    products.update(
      '${p['storeid']}:${p['productid']}',
      (value) => {
        ...value,
        ...p,
      },
      ifAbsent: () => {...p},
    );
  }

  // group products by `storeid`
  final storeProducts = <int, List<dynamic>>{};
  products.forEach((key, p) {
    storeProducts.update(
      p['storeid'],
      (value) => [...value, p],
      ifAbsent: () => [p],
    );
  });

  // build final `storeInfo` data
  final storeInfo = <String, dynamic>{
    'storeInfo': [
      for (final entry in storeProducts.entries)
        {
          'storeid': entry.key,
          'products': entry.value,
        }
    ],
  };

  return storeInfo;
}

void main() {
  final storeInfo = merge(json1, json2);
  JsonEncoder encoder = JsonEncoder.withIndent('  ');
  String prettyprint = encoder.convert(storeInfo);
  print(prettyprint);
}

{
  "storeInfo": [
    {
      "storeid": 91,
      "products": [
        {
          "storeid": 91,
          "productid": 37,
          "cartitemid": 48,
          "product_quantity": 8,
          "product_name": "hhh"
        }
      ]
    },
    {
      "storeid": 86,
      "products": [
        {
          "storeid": 86,
          "productid": 74,
          "cartitemid": 52,
          "product_quantity": 1
        },
        {
          "storeid": 86,
          "productid": 73,
          "cartitemid": 51,
          "product_quantity": 6,
          "product_name": "dfsdfsd"
        },
        {
          "storeid": 86,
          "productid": 76,
          "cartitemid": 50,
          "product_quantity": 1,
          "product_name": "dfsdfsd"
        }
      ]
    }
  ]
}

This looks indeed back-end stuff. But if you still need it the following code should do the trick on the front-end.

The merge function merges all properties from both products on json1 and json2 and then formats the output with the provided JSON template.

Check out the live demo on DartPad

import 'dart:convert';

final json1 = {
  "message": "success",
  "data": [
    {
      "storeid": "91",
      "productid": "37",
      "cartitemid": 48,
      "product_quantity": 8
    },
    {
      "storeid": "86",
      "productid": "74",
      "cartitemid": 52,
      "product_quantity": 1
    },
    {
      "storeid": "86",
      "productid": "73",
      "cartitemid": 51,
      "product_quantity": 6
    },
    {
      "storeid": "86",
      "productid": "76",
      "cartitemid": 50,
      "product_quantity": 1
    }
  ]
};

final json2 = {
  "data": [
    {"storeid": 91, "productid": 37, "product_name": "hhh"},
    {"storeid": 86, "productid": 73, "product_name": "dfsdfsd"},
    {"storeid": 86, "productid": 76, "product_name": "dfsdfsd"}
  ]
};

Map<String, dynamic> merge(
    Map<String, dynamic> cart, Map<String, dynamic> namedProducts) {
  final products = <String, Map<String, dynamic>>{};

  // include all cart products
  List<Map<String, dynamic>> cartData = cart['data'];
  for (final p in cartData) {
    final product = {
      ...p,
      'productid': int.parse(p['productid']),
      'storeid': int.parse(p['storeid']),
    };

    products.update(
      '${p['storeid']}:${p['productid']}',
      (value) => {
        ...value,
        ...product,
      },
      ifAbsent: () => product,
    );
  }

  // include all named products
  List<Map<String, dynamic>> namedProductsData = namedProducts['data'];
  for (final p in namedProductsData) {
    products.update(
      '${p['storeid']}:${p['productid']}',
      (value) => {
        ...value,
        ...p,
      },
      ifAbsent: () => {...p},
    );
  }

  // group products by `storeid`
  final storeProducts = <int, List<dynamic>>{};
  products.forEach((key, p) {
    storeProducts.update(
      p['storeid'],
      (value) => [...value, p],
      ifAbsent: () => [p],
    );
  });

  // build final `storeInfo` data
  final storeInfo = <String, dynamic>{
    'storeInfo': [
      for (final entry in storeProducts.entries)
        {
          'storeid': entry.key,
          'products': entry.value,
        }
    ],
  };

  return storeInfo;
}

void main() {
  final storeInfo = merge(json1, json2);
  JsonEncoder encoder = JsonEncoder.withIndent('  ');
  String prettyprint = encoder.convert(storeInfo);
  print(prettyprint);
}

OUTPUT

{
  "storeInfo": [
    {
      "storeid": 91,
      "products": [
        {
          "storeid": 91,
          "productid": 37,
          "cartitemid": 48,
          "product_quantity": 8,
          "product_name": "hhh"
        }
      ]
    },
    {
      "storeid": 86,
      "products": [
        {
          "storeid": 86,
          "productid": 74,
          "cartitemid": 52,
          "product_quantity": 1
        },
        {
          "storeid": 86,
          "productid": 73,
          "cartitemid": 51,
          "product_quantity": 6,
          "product_name": "dfsdfsd"
        },
        {
          "storeid": 86,
          "productid": 76,
          "cartitemid": 50,
          "product_quantity": 1,
          "product_name": "dfsdfsd"
        }
      ]
    }
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文