2

这是我的 JSON 代码:

{
    "query": {
        "count": 2,
        "created": "2013-04-03T09:47:03Z",
        "lang": "en-US",
        "results": {
            "yctCategories": {
                "yctCategory": {
                    "score": "0.504762",
                    "content": "Computing"
                }
            },
            "entities": {
                "entity": [
                    {
                        "score": "0.902",
                        "text": {
                            "end": "19",
                            "endchar": "19",
                            "start": "0",
                            "startchar": "0",
                            "content": "Computer programming"
                        },
                        "wiki_url": "http://en.wikipedia.com/wiki/Computer_programming"
                    },
                    {
                        "score": "0.575",
                        "text": {
                            "end": "51",
                            "endchar": "51",
                            "start": "41",
                            "startchar": "41",
                            "content": "programming"
                        }
                    }
                ]
            }
        }
    }
}

下面是我的PHP代码

$json_o = json_decode($json,true);
echo "Json result:</br>";
echo $json; // json
echo "</br></br>";
echo "Value result:</br>";
$result = array();
//$entity = $json_o['query']['results']['entities']['entity'];
foreach ($json_o['query']['results']['entities']['entity'] as $theentity)
 foreach ($theentity['text'] as $thetext){
    $result[] = $thetext['content'];
 }
print_r($result);

我的期望是在实体中获得内容的价值,即“计算机编程”和“编程”。

我已经四处寻找,但仍然找到了解决方案。

我的 PHP 代码的结果是:

Array ( [0] => 1 [1] => 1 [2] => 0 [3] => 0 [4] => C [5] => 5 [6] => 5 [7] => 4 [8] => 4 [9] => p ) 
4

4 回答 4

1

使用这个循环

foreach ($json_o['query']['results']['entities']['entity'] as $theentity)
{
    $result[] = $theentity['text']['content'];
}

http://codepad.viper-7.com/tFxh1w

输出数组([0] => 计算机编程 [1] => 编程)

于 2013-04-03T11:11:29.747 回答
0

删除第二个 foreach 并将其替换为$result[] = $theentity['text']['content'];

使用http://jsonlint.com/之类的内容可能会让您更轻松地了解 json 的结构。或者只是var_dump(或print_r)你的输出json_decode

于 2013-04-03T11:09:25.767 回答
0

使用简单的代码:

$array = $json_o['query']['results']['entities']['entity'];

foreach($array as $v){
  echo $v['text']['content'];
}
于 2013-04-03T11:12:21.290 回答
0

将您的 foreach 更改为:

$result = array();
foreach ($json_o['query']['results']['entities']['entity'] as $theentity) {
    $result[] = $theentity['text']['content'];
}
print_r($result);

$theentity['text']是一个键 => 值的数组。您可以只访问 keycontent而不是遍历所有条目。

您可以这样做的另一种方法(尽管这是一个糟糕的选择)是:

foreach($theentity['text'] as $key => $value) {
    if( 'content' === $key ) {
        $result[] = $value;
    }
}

我提供第二个示例来演示为什么原始代码不起作用。

更新

要访问其他属性,yctCategories只需执行类似操作

$categories = array();
foreach ($json_o['query']['results']['yctCategories'] as $yctCategory) {
    $categories[] = $yctCategory['content'];
}
print_r($categories);
于 2013-04-03T11:12:34.660 回答