如何从嵌套的JSON数据中获取值?

时间:2013-04-03 11:05:06

标签: php json

这是我的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 个答案:

答案 0 :(得分:1)

使用此循环

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

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

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

答案 1 :(得分:0)

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

使用类似http://jsonlint.com/的内容可能会让您更容易看到json的结构。或者只是var_dump(或print_rjson_decode的输出。

答案 2 :(得分:0)

使用简单的代码:

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

foreach($array as $v){
  echo $v['text']['content'];
}

答案 3 :(得分:0)

将您的foreach更改为:

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

$theentity['text']是一个键数组=&gt;值。您只需访问密钥content,而不是遍历所有条目。

你能做到的另一种方式(虽然这是一个糟糕的选择)是:

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);