正如其他人所说,您提供的 json 无效。
从它的结构来看,它的意图似乎是拥有一个对象数组,每个对象都有一个“类型”键和一个嵌入对象,其键名根据类型键的值而变化。(不同类型的键(在两个示例中,键都是“article”)包含一个标题,以及一个包含 twitter 和 facebook 计数的对象。)
两个注意事项:
- 从损坏的 json 中,我们不确定嵌入的文章对象是对象还是对象数组,因此我们将展示这两种情况的方法。
- 可能嵌入对象的键名是可变的,因此我们每次都需要从类型值中推导出它(但是在我们的示例中它始终是 'article'
现在要访问该结构,当您知道类型是文章时,以下内容是有效的(我们假设 $data 保存解码的 json):
$data[$i]['article']['title']
$data[$i]['article']['number']['facebook']
如果嵌入为对象数组,则上述内容应该是(访问第一个对象):
$data[$i]['article'][0]['title']
$data[$i]['article'][0]['number']['facebook']
但是我们可能需要推导出键名,因为它似乎是可变的,所以一般正确的形式是:
$typeName = $data[$i]['type']
$data[$i][$typeName]['title']
$data[$i][$typeName]['number']['facebook']
facebook的扩展形式是:$data[$i][$data[$i]['type']]['number']['facebook']
如果情况是“文章”作为对象数组嵌入,则:
$typeName = $data[$i]
$data[$i][$typeName][0]['title']
$data[$i][$typeName][0]['number']['facebook']
facebook的扩展形式是:$data[$i][$data[$i]['type']][0]['number']['facebook']
这是一个针对这两个 json 结构运行并访问它们的数据的脚本:
<?php
//Two possible jsons, it seems that:
//1)the embedded article object can be an object or an array of objects so we try two different json structures
//2)also it seems that the key of the embedded object is variable and thus we need to take it every time from the type value (however in our examples it is always 'article'
$json1 = '[{"type":"article", "article":{"title":"hello","number":{"facebook":4,"twitter":6}}},{"type":"article","article":{"title":"hello","number":{"facebook":1,"twitter":3}}}]';
$json2 = '[{"type":"article", "article":[{"title":"hello","number":{"facebook":4,"twitter":6}}]},{"type":"article","article":[{"title":"hello","number":{"facebook":1,"twitter":3}}]}]';
$data1 = json_decode($json1, true);
$data2 = json_decode($json2, true);
for ($i=0; $i<count($data1); $i++) {
$articleType = $data1[$i]['type'];
echo 'Title from element: ',$i, ' ', $data1[$i][$articleType]['title'],
PHP_EOL;
echo 'Facebook number from element ', $i, ' ',
$data1[$i][$articleType]['number']['facebook'], PHP_EOL;
echo 'Twitter number from element ', $i, ' ',
$data1[$i][$articleType]['number']['twitter'], PHP_EOL;
}
//However if we have embedded an array of objects then we access this way (hardcoded to index 0)
$embIndex = 0;
for ($i=0; $i<count($data2); $i++) {
$articleType = $data2[$i]['type'];
echo 'Title from element ',$i, ' and embedded object index ',
$embIndex, ': ',$data2[$i][$articleType][$embIndex]['title'], PHP_EOL;
echo 'Facebook number from element ', $i, ' and embedded object index ',
$embIndex, ': ', $data2[$i][$articleType][$embIndex]['number']['facebook'], PHP_EOL;
echo 'Twitter number from element ', $i, ' and embedded object index ',
$embIndex, ': ', $data2[$i][$articleType][$embIndex]['number']['twitter'], PHP_EOL;
}
和输出:
Title from element: 0 hello
Facebook number from element 0 4
Twitter number from element 0 6
Title from element: 1 hello
Facebook number from element 1 1
Twitter number from element 1 3
Title from element 0 and embedded object index 0: hello
Facebook number from element 0 and embedded object index 0: 4
Twitter number from element 0 and embedded object index 0: 6
Title from element 1 and embedded object index 0: hello
Facebook number from element 1 and embedded object index 0: 1
Twitter number from element 1 and embedded object index 0: 3