0

我在 php 中使用 json 提要时遇到问题。例如 :

[{"type":"article",
"article":[{
"title":"hello",
"number":{
"facebook":4,
"twitter":6}
}],

[{"type":"article",
"article":[{
"title":"hello",
"number":{
"facebook":1,
"twitter":3}
}],

我保存标题没有问题:

$titre = $data[$i]['type'][0]['title'];

但我找不到如何保存 Facebook 号码。我尝试了很多组合

$number = $data[0]['type'][$i]['scores']['facebook'][0];

或者

$number = $data[0]['type'][$i]['scores']['facebook'];

或者

$number = $data[0]['type'][$i]['scores']['facebook'][1];

没有人工作......你有什么想法吗?非常感谢你们!

4

3 回答 3

2

您的 json 无效。

http://php.net/manual/en/function.json-last-error.php

$json = '[{"type":"article",
  "article":[{
  "title":"hello",
  "number":{
  "facebook":4,
  "twitter":6}
  }],

  [{"type":"article",
  "article":[{
  "title":"hello",
  "number":{
  "facebook":1,
  "twitter":3}
  }]';
$array = json_decode($json, true);

switch (json_last_error()) {
  case JSON_ERROR_NONE:
    echo ' - No errors';
    break;
  case JSON_ERROR_DEPTH:
    echo ' - Maximum stack depth exceeded';
    break;
  case JSON_ERROR_STATE_MISMATCH:
    echo ' - Underflow or the modes mismatch';
    break;
  case JSON_ERROR_CTRL_CHAR:
    echo ' - Unexpected control character found';
    break;
  case JSON_ERROR_SYNTAX:
    echo ' - Syntax error, malformed JSON';
    break;
  case JSON_ERROR_UTF8:
    echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
  default:
    echo ' - Unknown error';
    break;
}

print_r($array);
  • 语法错误,格式错误的 JSON
于 2013-07-22T12:56:12.550 回答
0

尝试使用 json_decode() 函数(php 手动输入)。通过这种方式,您可以轻松访问 json 的值,避免在尝试查找数组中的值时头疼。

如果您的 Json 无效或有问题(这有助于调试),这也将返回 NULL

于 2013-07-22T12:56:14.817 回答
0

正如其他人所说,您提供的 json 无效。

从它的结构来看,它的意图似乎是拥有一个对象数组,每个对象都有一个“类型”键和一个嵌入对象,其键名根据类型键的值而变化。(不同类型的键(在两个示例中,键都是“article”)包含一个标题,以及一个包含 twitter 和 facebook 计数的对象。)

两个注意事项:

  1. 从损坏的 json 中,我们不确定嵌入的文章对象是对象还是对象数组,因此我们将展示这两种情况的方法。
  2. 可能嵌入对象的键名是可变的,因此我们每次都需要从类型值中推导出它(但是在我们的示例中它始终是 '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
于 2013-07-22T14:13:25.150 回答