簡體   English   中英

如何從嵌套的JSON數據獲取值?

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

這是我的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 ) 

使用這個循環

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

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

輸出數組([0] =>計算機編程[1] =>編程)

刪除第二個foreach並替換為$result[] = $theentity['text']['content'];

使用http://jsonlint.com/之類的內容可能會使您更輕松地了解json的結構。 或者只是var_dump (或print_r )您的json_decode的輸出。

使用簡單的代碼:

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

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

將您的foreach更改為:

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

$theentity['text']是一個鍵數組=> value。 您僅可以訪問關鍵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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM