如何从现有的json字符串构建一个新的json字符串

时间:2019-02-13 21:39:09

标签: php json ajax

我有多个文件,其中包含如下所示的json数据:

1.json:

[{
    "id": 2100059,
    "email": "",
    "company": "acme",
    "job_title": "",
    "first_name": "Jane",
    "last_name": "Doe"
 }, 
 {
    "id": 2199991,
    "email": "",
    "company": "Widgets Inc",
    "job_title": "",
    "first_name": "John",
    "last_name": "Doe"
 }]

2.json:

[{
    "id": 123456,
    "email": "",
    "company": "acme",
    "job_title": "",
    "first_name": "GI Jame",
    "last_name": "Adf"
 }, 
 {
    "id": 000001,
    "email": "",
    "company": "Widgets Inc",
    "job_title": "",
    "first_name": "bob",
    "last_name": "billy"
 }]

如何合并这两个列表,使其仅是一个字符串,并带有一组[]?

到目前为止,我将每个文件的内容中读取的逻辑写入单独的字符串中。但是后来我想我应该: 1. json_decode()每个字符串都将其视为对象

虽然不确定如何附加到对象。 任何提示将不胜感激。

编辑1

我决定将每个文件的内容读入一个数组,然后尝试下面建议的技巧来使用array_merge。当我在数组上执行print_r时,它看起来像这样:(伪造的数据,但您应该有个主意)

Array
(
    [0] => [{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}]
    [1] => [{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}]

    [2] => [{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}
    [3] =>[{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}]
)

然后这就是我试图将所有这些组合成一个json字符串的逻辑:

    print_r($allpages);
    $finaljsonstring ='';
    foreach ($allpages as $item)
    {
        $finaljsonstring = $finaljsonstring + json_encode(array_merge(json_decode($item)));
    }
    echo $finaljsonstring;

但是出了点问题。 (我将其称为ajax,因此很难调试),但是当我console.log从ajax调用得到的结果时,什么也没有返回。 最终,我需要返回到前端的是单个json字符串数组,如下所示:

[{},{},{}]

1 个答案:

答案 0 :(得分:1)

您可以使用json_decode()array_merge()来实现所需的功能。

<?php
$json_1 = <<<EOD
[{
    "id": 2100059,
    "email": "",
    "company": "acme",
    "job_title": "",
    "first_name": "Jane",
    "last_name": "Doe"
 }, 
 {
    "id": 2199991,
    "email": "",
    "company": "Widgets Inc",
    "job_title": "",
    "first_name": "John",
    "last_name": "Doe"
 }]
EOD;

$json_2 = <<<EOD
[{
    "id": 123456,
    "email": "",
    "company": "acme",
    "job_title": "",
    "first_name": "GI Jame",
    "last_name": "Adf"
 }, 
 {
    "id": 1122,
    "email": "",
    "company": "Widgets Inc",
    "job_title": "",
    "first_name": "bob",
    "last_name": "billy"
 }]
EOD;

示例:

$json_decoded_1 = json_decode($json_1);

$json_decoded_2 = json_decode($json_2);

$arr_combined = array_merge( $json_decoded_1, $json_decoded_2 );

$json_combined = json_encode($arr_combined);

echo $json_combined;

请注意,您不必传递true上的第二个json_decode()参数即可将字符串转换为数组。使用array_merge()时,这些项目可以保留为对象。

http://php.net/manual/en/function.array-merge.php

http://php.net/manual/en/function.json-decode.php