如何将两个JSON响应合并为一个结构中的一个
此响应来自一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该输出模型必须在API输出级别上构建。
This output model must be built at the api output level.
这确实是后端的东西。但是,如果您仍然需要它,则以下代码应在前端方面解决问题。
MERGE
函数在JSON1
和JSON2
上合并了两个产品的所有属性,然后使用提供的JSON模板格式化输出。查看
”
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 onjson1
andjson2
and then formats the output with the provided JSON template.Check out the live demo on DartPad
OUTPUT