0

我有多个 JSON 文件,我想对其进行解析、编辑和合并到一个对象中,最终将其重新编码并输出为单个 JSON。

我目前有一个关于何时解析和编辑一个对象的工作示例,但我不知道如何做两个并获得一个结果。

工作示例:

$source = 'http://www.jakedup.com/_/source1';

$data = json_decode(file_get_contents($source));

// THIS WILL BE THE FINAL OUTPUT OBJECT
$output = new stdClass();

// GET THE ARTICLE DATES AS AN ARRAY
$tracks = get_object_vars($data->tracks);

// LOOPS OVER ARTICLE PUBDATES TO GET THE OUTER OBJECTS
foreach ($tracks as $key=>$val) {
  // LOOPS OVER THE INNER OBJECTS IN EACH ARTICLE PUBDATE
  foreach ($data->tracks->$key as $object) {
    // ADD IT ONTO THE OUTPUT OBJECT WITH "ID" AS PARENT NAME
    $output->{$object->id} = $object;
  }
}

echo json_encode($output);

这是我的 JSON 示例。我提供了两个示例,但我正在寻找一种动态合并无限数量的 JSON 文件的方法。

JSON 源 1:

{
    "foo":"bar",
    "tracks":{
        "1349496000":[
            {
                "id":"25328",
                "is_promo":null,
                "is_feature":null,
                "listens":"1312",
                "downloads":"777"
            },
            {
                "id":"25327",
                "is_promo":null,
                "is_feature":null,
                "listens":"1255",
                "downloads":"578"
            },
            {
                "id":"25329",
                "is_promo":null,
                "is_feature":null,
                "listens":"341",
                "downloads":"139"
            }
        ],
        "1349323200":[
            {
                "id":"25310",
                "is_promo":null,
                "is_feature":null,
                "listens":"10793",
                "downloads":"4953"
            }
        ]
    }
}

JSON 源 2:

{
    "foo":"bar",
    "tracks":{
        "1349323200":[
            {
                "id":"25303",
                "is_promo":null,
                "is_feature":null,
                "listens":"2347",
                "downloads":"1499"
            }
        ],
        "1349236800":[
            {
                "id":"25266",
                "is_promo":null,
                "is_feature":null,
                "listens":"24485",
                "downloads":"19366"
            }
        ]
    }
}

我一直在玩弄循环获取源的想法,但是在合并数据时我被卡住了。我所有的不同努力都没有产生正确的数据结构。

当前测试代码:

$source = 'http://www.jakedup.com/_/source';
$pages = 2;

    for ($i = 1; $i <= $pages; $i++) {
        $sources[$i] = $source.$i;
        ${'source'.$i} = json_decode(file_get_contents($source.$i));
    }

我现在有两个对象——$source1 和 $source2——但我不确定下一步该做什么。

这是我期待的输出:

{
    "25328": {
        "id": "25328",
        "is_promo": null,
        "is_feature": null,
        "listens": "1312",
        "downloads": "777",
        "timestamp": "1349496000"
    },
    "25327": {
        "id": "25327",
        "is_promo": null,
        "is_feature": null,
        "listens": "1255",
        "downloads": "578",
        "timestamp": "1349496000"
    },
    "25329": {
        "id": "25329",
        "is_promo": null,
        "is_feature": null,
        "listens": "341",
        "downloads": "139",
        "timestamp": "1349496000"
    },
    "25310": {
        "id": "25310",
        "is_promo": null,
        "is_feature": null,
        "listens": "10793",
        "downloads": "4953",
        "timestamp": "1349323200"
    },
    "25303": {
        "id": "25303",
        "is_promo": null,
        "is_feature": null,
        "listens": "2347",
        "downloads": "1499",
        "timestamp": "1349323200"
    },
    "25266": {
        "id": "25266",
        "is_promo": null,
        "is_feature": null,
        "listens": "24485",
        "downloads": "19366",
        "timestamp": "1349236800"
    }
}

在此先感谢您的帮助!

4

1 回答 1

4

使用 PHP 数组比尝试stdClass使用数字属性构建对象更简单一些。如果 PHP 数组具有非连续的、基于非零的键,json_encode()将自动生成一个对象{}而不是数组。[]同样json_decode()有一个可选的第二个参数来产生一个关联数组而不是一个对象。所以我建议将文件解码为一个数组并循环它们,将每个文件附加id到一个输出数组上,你最终会json_encode()再次使用它。

您的代码暗示您有 JSON 源文件,例如“http://www.jakedup.com/_/source1”。您在正确的轨道上,但不是填充新的变量变量,而是在循环中检索和解码每个文件,并将其直接附加到输出数组。

// 5 source numbers, don't have to be consecutive
$sources = array(1,2,3,4,5);
// Output array to be encoded as JSON
$output = array();

// Loop over all the source files, retrieve them and add tracks onto the output    
foreach ($sources as $sourcenum) {
  $source = "http://www.jakedup.com/_/source$sourcenum";
  $source_json = file_get_contents($source);

  // Decode it as an associative array, passing TRUE as second param
  $source_array = json_decode($source_json, TRUE);

  // Now loop over the tracks (which are an array) and append each to the output
  // the original key looks like a timestamp
  foreach ($source_array['tracks'] as $timestamp => $ts) {
    foreach ($ts as $track) {
      // Add the track id to $output as a key
      // This doesn't check if the id already exists...
      // if it does, it will just be overwritten with this one
      $output[$track['id']] = $track;
      // Optionally, save the original timestamp key into the track array
      // Maybe you don't care about this
      $output[$track['id']]['timestamp'] = $timestamp;
    }
  }
}

// Look over your array
var_dump($output);

// Finally, re-encode the whole thing back to JSON
// using JSON_FORCE_OBJECT (even though it shouldn't be necessary)
$output_json = json_encode($output, JSON_FORCE_OBJECT);

JSON输出:

{
    "25328": {
        "id": "25328",
        "is_promo": null,
        "is_feature": null,
        "listens": "1312",
        "downloads": "777",
        "timestamp": 1349496000
    },
    "25327": {
        "id": "25327",
        "is_promo": null,
        "is_feature": null,
        "listens": "1255",
        "downloads": "578",
        "timestamp": 1349496000
    },
    "25329": {
        "id": "25329",
        "is_promo": null,
        "is_feature": null,
        "listens": "341",
        "downloads": "139",
        "timestamp": 1349496000
    },
    "25310": {
        "id": "25310",
        "is_promo": null,
        "is_feature": null,
        "listens": "10793",
        "downloads": "4953",
        "timestamp": 1349323200
    },
    "25303": {
        "id": "25303",
        "is_promo": null,
        "is_feature": null,
        "listens": "2347",
        "downloads": "1499",
        "timestamp": 1349323200
    },
    "25266": {
        "id": "25266",
        "is_promo": null,
        "is_feature": null,
        "listens": "24485",
        "downloads": "19366",
        "timestamp": 1349236800
    }
}
于 2012-10-08T15:47:44.393 回答