我如何从多个相同的获取请求中连接/合并多个 json?

Posted

技术标签:

【中文标题】我如何从多个相同的获取请求中连接/合并多个 json?【英文标题】:How i can concatenate/merge multiple json from multiple same get request? 【发布时间】:2022-01-06 05:45:21 【问题描述】:

我需要将获得的各种答案与在同一个 API 上获得的结果合并。 我需要使用 javascript 我重申,每次 GET API 调用我都会得到相同的格式。我只需要在最后得到一个 json,如问题末尾所示。 例如第一个请求让我:

    
        "data": [
            "id": 10896584,
            "recipient_id": "+393881903818",
            "delivery_status": "delivered",
            "verbose_delivery_status": "Delivered",
            "direction": "OUT",
            "country": "IT",
            "price": "0.000",
            "is_two_way": false,
            "receive_dlr": "",
            "webhook_url": "",
            "created_at": "2021-10-28T12:48:44+00:00"
        ]

第二个请求给我:


    "data": [
        "id": 29384883,
        "recipient_id": "+393881903818",
        "delivery_status": "delivered",
        "verbose_delivery_status": "Delivered",
        "direction": "OUT",
        "country": "IT",
        "price": "0.000",
        "is_two_way": false,
        "receive_dlr": "",
        "webhook_url": "",
        "created_at": "2021-10-28T12:48:44+00:00"
    ]
 

所以我想合并这两个响应并得到一个像这样的 json:

  
        "data": [
            "id": 10896584,
            "recipient_id": "+393881903818",
            "delivery_status": "delivered",
            "verbose_delivery_status": "Delivered",
            "direction": "OUT",
            "country": "IT",
            "price": "0.000",
            "is_two_way": false,
            "receive_dlr": "",
            "webhook_url": "",
            "created_at": "2021-10-28T12:48:44+00:00"
            ,
            
            "id": 29384883,
            "recipient_id": "+393881903818",
            "delivery_status": "delivered",
            "verbose_delivery_status": "Delivered",
            "direction": "OUT",
            "country": "IT",
            "price": "0.000",
            "is_two_way": false,
            "receive_dlr": "",
            "webhook_url": "",
            "created_at": "2021-10-28T12:48:44+00:00"
            
        ]
   

【问题讨论】:

没有足够的详细信息,例如您使用的后端技术。据我了解,您可以从这里获取想法:***.com/questions/65427398/… 我需要用纯javascript来做,因为它是一个简单的一次性手动操作。我对我的端点进行了提取操作,然后我得到了这些数据。 【参考方案1】:

这样的事情应该可以工作:

const response1 =    
        "data": [
            "id": 10896584,
            "recipient_id": "+393881903818",
            "delivery_status": "delivered",
            "verbose_delivery_status": "Delivered",
            "direction": "OUT",
            "country": "IT",
            "price": "0.000",
            "is_two_way": false,
            "receive_dlr": "",
            "webhook_url": "",
            "created_at": "2021-10-28T12:48:44+00:00"
        ]
;

const response2 = 
    "data": [
        "id": 29384883,
        "recipient_id": "+393881903818",
        "delivery_status": "delivered",
        "verbose_delivery_status": "Delivered",
        "direction": "OUT",
        "country": "IT",
        "price": "0.000",
        "is_two_way": false,
        "receive_dlr": "",
        "webhook_url": "",
        "created_at": "2021-10-28T12:48:44+00:00"
    ]
 ;
 
const result = JSON.parse(JSON.stringify(response1));

result.data.push(...response2.data);

console.log(result);

【讨论】:

以上是关于我如何从多个相同的获取请求中连接/合并多个 json?的主要内容,如果未能解决你的问题,请参考以下文章

从jQuery中的多个相同的类名中获取值CSS

在 F# 中“合并”多个相同长度的列表的惯用方式?

如何在没有实体的原则查询中合并连接表

React - 多个 Axios 请求

如何通过 id 合并多个列表并获取特定数据?

如何在没有实体的学说查询中合并连接表