使用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", } ] } } { “foo”:“bar”, "公安条例":一,, “boo”:67, “条款”:{ "1329800400":[ { “id”:“234”, “标题”:“本文标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“287.3200”, }, { “id”:“212”, “标题”:“另一篇文章标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“199.0530”, }, { “id”:“196”, “标题”:“第三篇文章标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“217.0250”, } ], "1329714000":[ { “id”:“176”, “标题”:“另一篇文章标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “期限”:“219.6890”, }, { “id”:“155”, “标题”:“最后一篇文章的标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“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" } } { "1": { “id”:“234”, “标题”:“本文标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“287.3200”, “发布日期”:“1329800400” }, "2": { “id”:“212”, “标题”:“另一篇文章标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“199.0530”, “发布日期”:“1329800400” }, "3": { “id”:“196”, “标题”:“第三篇文章标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“217.0250”, “发布日期”:“1329800400” }, "4": { “id”:“176”, “标题”:“另一篇文章标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “期限”:“219.6890”, “发布日期”:“1329714000” }, "5": { “id”:“155”, “标题”:“最后一篇文章的标题”, “url:“http:\/\/www.test.com\/path to article.html”, “thumb”:“http:\/\/www.test.com\/path to image.jpg”, “附件”:“http:\/\/www.test.com\/path to attachment.mp3”, “持续时间”:“228.2570”, “发布日期”:“1329714000” } }使用PHP解码JSON并格式化父/子结构?,php,json,Php,Json,我从外部站点获得了以下JSON输出: { "foo":"bar", "poo":1, "boo":67, "articles":{ "1329800400":[ { "id":"234", "title":"Title of This Article", "url":"http:\/\/www.tes
这有多难?请注意pubDate是如何从父对象变成子对象的。还要记住,我永远不会知道pubDates的值——它们每天都在变化。提前感谢您的建议 开始的JSON字符串有一个问题,因为它在
duration
属性之后有尾随逗号,这些属性无效。因此,首先,需要删除那些尾随的逗号
$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
// Function lifted from PHP docs...
function removeTrailingCommas($json) {
$json=preg_replace('/,\s*([\]}])/m', '$1', $json);
return $json;
}
articles
是具有数字属性名称的对象,因此需要使用get\u object\u 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对象转储:
JSON字符串:
嗯,这似乎不是我想要的。我需要一个与示例中给出的格式完全相同的输出。注意它是如何简化的。在尝试了你的代码之后,我得到了NULL的返回值。很抱歉我的速度太慢了——我对PHP非常陌生!对不起,我只是有点困惑,你已经移动了发布日期,但是其他的斜杠更改是有意的吗?您的输出不是有效的json,因为它是现在的。我完全更改了它。现在重试吗?对不起,使用斜杠进行的更改不是有意的。我修复了这个操作。尝试了你的新代码,我得到了以下错误:“警告:在json2.php的第11行[]上为foreach()提供的参数无效”-第11行是“foreach($item=>$pubdate\u unixtime){”+1这个答案感觉比我自己的答案彻底得多,而且总体上比我自己的答案好得多-我确实说过我对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)
}
}
{"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}
}