使用PHP解码JSON并格式化父/子结构?

时间:2012-02-22 07:24:09

标签: php json

我从外部网站获得以下JSON输出:


    {
       "foo":"bar",
       "poo":1,
       "boo":67,
       "articles":{
          "1329800400":[
             {
                "id":"234",
                "title":"Title of This Article",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
                "duration":"287.3200",
             },
             {
                "id":"212",
                "title":"Another Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
                "duration":"199.0530",
             },
             {
                "id":"196",
                "title":"A Third Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
                "duration":"217.0250",
             }
          ],
          "1329714000":[
             {
                "id":"176",
                "title":"Yet Another Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
                "duration":"219.6890",
             },
             {
                "id":"155",
                "title":"The Last Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
                "duration":"228.2570",
             }
          ]
       }
    }

我想通过PHP获取文件,并将结构重新格式化为:


    {
        "1": {
            "id":"234",
            "title":"Title of This Article",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
            "duration":"287.3200",
            "pubDate":"1329800400"
        },
        "2": {
            "id":"212",
            "title":"Another Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
            "duration":"199.0530",
            "pubDate":"1329800400"
        },
        "3": {
            "id":"196",
            "title":"A Third Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
            "duration":"217.0250",
            "pubDate":"1329800400"
        },
        "4": {
            "id":"176",
            "title":"Yet Another Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
            "duration":"219.6890",
            "pubDate":"1329714000"
        },
        "5": {
            "id":"155",
            "title":"The Last Article Title",
                "url":"http:\/\/www.test.com\/path-to-article.html",
                "thumb":"http:\/\/www.test.com\/path-to-image.jpg",
                "attachment":"http:\/\/www.test.com\/path-to-attachment.mp3",
            "duration":"228.2570",
            "pubDate":"1329714000"
        }
    }

这有多难?注意pubDate如何从父母变为孩子。还要记住,我永远不会知道pubDates的价值 - 它们每天都在变化。提前感谢任何建议!

2 个答案:

答案 0 :(得分:2)

启动JSON字符串存在问题,因为它在duration属性之后有逗号,这些逗号无效。首先,需要删除那些尾随逗号。

// Function lifted from PHP docs...
function removeTrailingCommas($json) {
   $json=preg_replace('/,\s*([\]}])/m', '$1', $json);
    return $json;
}

articles是具有数字属性名称的对象,因此需要使用get_object_vars()检索这些对象以便于迭代。

// Get rid of trailing commas
$json = json = removeTrailingCommas($json);

// Decode your string (already in $json)
$obj = json_decode($json);

// Get the article dates as an array
$articles = (get_object_vars($obj->articles));

// This will be the final output object
$output = new stdClass();
// Since they will be numeric properties starting at 1...
$outkey = 1;
// Loops over article pubdates to get the outer objects
foreach ($articles as $key=>$val) {
  // Loops over the inner objects in each article pubdate
  foreach ($obj->articles->$key as $sub_obj) {

    // Copies the object and adds the pubdate property
    $tmp = clone $sub_obj;
    $tmp->pubDate = $key;

    // Add it onto the output object
    $output->$outkey = $tmp;
    $outkey++;
  }
}
//var_dump($output);
echo json_encode($output);

PHP对象转储:

object(stdClass)#8 (5) {
  ["0"]=>
  object(stdClass)#9 (7) {
    ["id"]=>
    string(3) "234"
    ["title"]=>
    string(21) "Title of This Article"
    ["url"]=>
    string(40) "http://www.test.com/path-to-article.html"
    ["thumb"]=>
    string(37) "http://www.test.com/path-to-image.jpg"
    ["attachment"]=>
    string(42) "http://www.test.com/path-to-attachment.mp3"
    ["duration"]=>
    string(8) "287.3200"
    ["pubDate"]=>
    int(1329800400)
  }
  ["1"]=>
  object(stdClass)#10 (7) {
    ["id"]=>
    string(3) "212"
    ["title"]=>
    string(21) "Another Article Title"
    ["url"]=>
    string(40) "http://www.test.com/path-to-article.html"
    ["thumb"]=>
    string(37) "http://www.test.com/path-to-image.jpg"
    ["attachment"]=>
    string(42) "http://www.test.com/path-to-attachment.mp3"
    ["duration"]=>
    string(8) "199.0530"
    ["pubDate"]=>
    int(1329800400)
  }
  ["2"]=>
  object(stdClass)#11 (7) {
    ["id"]=>
    string(3) "196"
    ["title"]=>
    string(21) "A Third Article Title"
    ["url"]=>
    string(40) "http://www.test.com/path-to-article.html"
    ["thumb"]=>
    string(37) "http://www.test.com/path-to-image.jpg"
    ["attachment"]=>
    string(42) "http://www.test.com/path-to-attachment.mp3"
    ["duration"]=>
    string(8) "217.0250"
    ["pubDate"]=>
    int(1329800400)
  }
  ["3"]=>
  object(stdClass)#12 (7) {
    ["id"]=>
    string(3) "176"
    ["title"]=>
    string(25) "Yet Another Article Title"
    ["url"]=>
    string(40) "http://www.test.com/path-to-article.html"
    ["thumb"]=>
    string(37) "http://www.test.com/path-to-image.jpg"
    ["attachment"]=>
    string(42) "http://www.test.com/path-to-attachment.mp3"
    ["duration"]=>
    string(8) "219.6890"
    ["pubDate"]=>
    int(1329714000)
  }
  ["4"]=>
  object(stdClass)#13 (7) {
    ["id"]=>
    string(3) "155"
    ["title"]=>
    string(22) "The Last Article Title"
    ["url"]=>
    string(40) "http://www.test.com/path-to-article.html"
    ["thumb"]=>
    string(37) "http://www.test.com/path-to-image.jpg"
    ["attachment"]=>
    string(42) "http://www.test.com/path-to-attachment.mp3"
    ["duration"]=>
    string(8) "228.2570"
    ["pubDate"]=>
    int(1329714000)
  }
}

JSON字符串:

{"0":
    {"id":"234",
     "title":"Title of This Article",
     "url":"http://www.test.com/path-to-article.html",
     "thumb":"http://www.test.com/path-to-image.jpg",
     "attachment":"http://www.test.com/path-to-attachment.mp3",
     "duration":"287.3200",
     "pubDate":1329800400},
 "1":
    {"id":"212",
     "title":"Another Article Title",
     "url":"http://www.test.com/path-to-article.html",
     "thumb":"http://www.test.com/path-to-image.jpg",
     "attachment":"http://www.test.com/path-to-attachment.mp3",
     "duration":"199.0530",
     "pubDate":1329800400},
 "2":
    {"id":"196",
     "title":"A Third Article Title",
     "url":"http://www.test.com/path-to-article.html",
     "thumb":"http://www.test.com/path-to-image.jpg",
     "attachment":"http://www.test.com/path-to-attachment.mp3",
     "duration":"217.0250",
     "pubDate":1329800400},
 "3":
    {"id":"176",
     "title":"Yet Another Article Title",
     "url":"http://www.test.com/path-to-article.html",
     "thumb":"http://www.test.com/path-to-image.jpg",
     "attachment":"http://www.test.com/path-to-attachment.mp3",
     "duration":"219.6890",
     "pubDate":1329714000},
 "4":
    {"id":"155",
     "title":"The Last Article Title",
     "url":"http://www.test.com/path-to-article.html",
     "thumb":"http://www.test.com/path-to-image.jpg",
     "attachment":"http://www.test.com/path-to-attachment.mp3",
     "duration":"228.2570",
     "pubDate":1329714000}
}

答案 1 :(得分:0)

$jsondata= json_decode($filedata); //Filedata contains your JSON
$reformat = $jsondata['articles'];  //Get only the articles section
$tmp = array(); //Temporary array to store the new JSON data
$i=0; //Create a counter and start at 0

foreach($reformat as $item=>$pubdate_unixtime) {
    $i++;  //Add 1 to the counter
    $item["pubDate"] = $pubdate_unixtime; //Add the pubdate to the array
    $tmp[$i] = $item; //Add the old array to a new one and change the key
}

echo json_encode($tmp); //Change the php to json