Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将两个JSON合并为一个_Javascript_Json_React Native - Fatal编程技术网

Javascript 将两个JSON合并为一个

Javascript 将两个JSON合并为一个,javascript,json,react-native,Javascript,Json,React Native,我正试图将两个JSON合并为一个,但我做不到。我试着用stringify将它们转换成字符串,连接它们,并用parse重做JSON。。。但它不起作用 这是我的代码(ordersByCustomerId返回一个JSON对象): 我想把两个相同的连接在一起 问候 编辑: 那么,如果我有两个像这样的JSON,我如何合并它们呢 [{ "id": 1523, "parent_id": 0, "number": "1523", &

我正试图将两个JSON合并为一个,但我做不到。我试着用stringify将它们转换成字符串,连接它们,并用parse重做JSON。。。但它不起作用

这是我的代码(ordersByCustomerId返回一个JSON对象):

我想把两个相同的连接在一起

问候

编辑:

那么,如果我有两个像这样的JSON,我如何合并它们呢

[{
"id": 1523,
"parent_id": 0,
"number": "1523",
"order_key": "wc_order_",
"created_via": "rest-api",
"version": "4.0.0",
"status": "processing",
"currency": "EUR",
"date_created": "2020-09-03T10:24:44",
"date_created_gmt": "2020-09-03T08:24:44",
"date_modified": "2020-09-03T10:24:45",
"date_modified_gmt": "2020-09-03T08:24:45",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "26.33",
"total": "151.72",
"total_tax": "26.33",
"prices_include_tax": true,
"customer_id": 11,
"customer_ip_address": "",
"customer_user_agent": "",
"customer_note": "",
"billing": {
    "first_name": "name",
    "last_name": "last name",
    "company": "",
    "address_1": "address",
    "address_2": "",
    "city": "city",
    "state": "M",
    "postcode": "12345",
    "country": "ES",
    "email": "email@email.com",
    "phone": "(34) 123123123"
},
"shipping": {
    "first_name": "first name",
    "last_name": "last name",
    "company": "",
    "address_1": "address",
    "address_2": "",
    "city": "city",
    "state": "state",
    "postcode": "12345",
    "country": ""
},
"payment_method": "cod",
"payment_method_title": "cod",
"transaction_id": "",
"date_paid": null,
"date_paid_gmt": null,
"date_completed": null,
"date_completed_gmt": null,
"cart_hash": "",
"meta_data": [

],
"line_items": [{
    "id": 469,
    "name": "test product",
    "product_id": 1197,
    "variation_id": 0,
    "quantity": 4,
    "tax_class": "",
    "subtotal": "125.39",
    "subtotal_tax": "26.33",
    "total": "125.39",
    "total_tax": "26.33",
    "taxes": [{
        "id": 3,
        "total": "26.33157",
        "subtotal": "26.33157"
    }],
    "meta_data": [

    ],
    "sku": "1234",
    "price": 31.3471075
}],
"tax_lines": [{
    "id": 471,
    "rate_code": "ES-IVA (CLIENTE FINAL)-1",
    "rate_id": 3,
    "label": "IVA (cliente final)",
    "compound": false,
    "tax_total": "26.33",
    "shipping_tax_total": "0.00",
    "rate_percent": 21,
    "meta_data": [

    ]
}],
"shipping_lines": [{
    "id": 470,
    "method_title": "Envío gratuito",
    "method_id": "free_shipping",
    "instance_id": "2",
    "total": "0.00",
    "total_tax": "0.00",
    "taxes": [{
        "id": 3,
        "total": "0",
        "subtotal": ""
    }],
    "meta_data": [

    ]
}],
"fee_lines": [

],
"coupon_lines": [

],
"refunds": [

],
"currency_symbol": "€",
"_links": {
    "self": [{
        "href": ""
    }],
    "collection": [{
        "href": ""
    }],
    "customer": [{
        "href": ""
    }]
}
}]

看起来您实际上是在处理JavaScript对象,而不是JSON。这在JavaScript中可能有点混乱。你可以用一种类似的方式来处理它们,因为它们很容易在两种表示之间移动。Krzysztof Krzeszewski在评论中提到的重要一点是,JSON是用于数据交换的文本格式

以下是有效的JSON:

{"key": "val"}
它也恰好是一个有效的JavaScript对象。因此,当通过HTTP请求发送JSON时,我们通常会将响应转换为JavaScript对象,以便在前端(如果使用nodejs,则在后端)轻松使用

在本例中,看起来这是为您完成的,因此您实际上有两个希望合并在一起的JavaScript对象。这可以通过以下方式完成。例如:

let objOne = { keyOne: "val1" };
let objTwo = { keyTwo: "val2" };

let result = {};
Object.assigns(result, objOne, objTwo);

console.log(result);
// { keyOne: "val1", keyTwo: "val2" }
这将获取一个目标对象,然后获取多个源对象,并将属性指定给目标对象。它按照您将目标交给函数的顺序执行此操作,这意味着如果objTwo有一个名为“keyOne”的键,其值为“val3”,那么您将得到以下结果:

{ keyOne: "val3", keyTwo: "val2" }

值得注意的是,这不是一个“深度”合并。它将只执行顶级属性。如果要进行深度合并,您需要循环相关对象,或者查看如何使用库中的函数,例如。

JSON!==JSON是用于数据交换的文本表示法。如果您处理的是JavaScript源代码,而不是字符串,那么您就不是在处理JSON。我想我更明白一点。。。但我是JavaScript新手,我有点迷路了。。。我用一个JSON示例编辑了主要帖子,我得到了。。。非常感谢您的帮助:)您好@pmj,我认为我的建议会奏效,尽管您可能希望像我最后建议的那样使用lodash之类的东西进行深度合并。请注意,您的两个JSON对象都在一个数组中,因此您需要将它们从数组中拉出并进行合并(或者遍历数组并创建一个新的合并对象数组)。
{ keyOne: "val3", keyTwo: "val2" }