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

2 番目の foreach を削除し、次のように置き換えます$result[] = $theentity['text']['content'];

http://jsonlint.com/のようなものを使用すると、json がどのように構造化されているかを簡単に確認できる場合があります。あるいは、単にvar_dump(またはprint_r)あなたの出力json_decode

于 2013-04-03T11:09:25.767 に答える