简体   繁体   English

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

[英]How to get values from nested JSON data?

Here's my JSON code: 这是我的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"
                        }
                    }
                ]
            }
        }
    }
}

and below is my PHP code 下面是我的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);

My expectation is to get the value of content in entity, which is " Computer programming " and " programming ". 我的期望是在实体中获取内容的价值,即“ 计算机编程 ”和“ 编程 ”。

I already searching around, but still have found the solution yet. 我已经在搜索,但是仍然找到了解决方案。

The result of my PHP code is: 我的PHP代码的结果是:

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

Use this loop 使用这个循环

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

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

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

remove the second foreach and replace it with $result[] = $theentity['text']['content']; 删除第二个foreach并替换为$result[] = $theentity['text']['content'];

using something like http://jsonlint.com/ might make it easier for you to see how the json is structured. 使用http://jsonlint.com/之类的内容可能会使您更轻松地了解json的结构。 alternatively just var_dump (or print_r ) the output of your json_decode . 或者只是var_dump (或print_r )您的json_decode的输出。

Use simple code: 使用简单的代码:

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

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

Change your foreach to: 将您的foreach更改为:

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

$theentity['text'] is an array of keys => value. $theentity['text']是一个键数组=> value。 You can just access key content instead of looping through all of the entries. 您仅可以访问关键content而不必循环浏览所有条目。

Another way you could do it (though it is a poor choice) is: 您可以执行的另一种方式(尽管这是一个糟糕的选择)是:

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

I provide this second example to demonstrate why the original code did not work. 我提供了第二个示例,以说明原始代码为何不起作用。

Update 更新

To access other properties like the yctCategories just do something similar 要访问yctCategories等其他属性,只需执行类似的操作

$categories = array();
foreach ($json_o['query']['results']['yctCategories'] as $yctCategory) {
    $categories[] = $yctCategory['content'];
}
print_r($categories);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM