[英]how to decode a json in php
這是我的json,
{
"responseHeader":{
"status":0,
"QTime":15},
"response":{"numFound":0,"start":0,"docs":[]
},
"spellcheck":{
"suggestions":[
"abert",{
"numFound":10,
"startOffset":0,
"endOffset":5,
"origFreq":0,
"suggestion":[{
"word":"albert",
"freq":126},
{
"word":"aber t",
"freq":317},
{
"word":"alert",
"freq":58},
{
"word":"a bert",
"freq":13},
{
"word":"abort",
"freq":57},
{
"word":"a be rt",
"freq":1045},
{
"word":"avert",
"freq":37},
{
"word":"ab e rt",
"freq":335},
{
"word":"aberr",
"freq":21},
{
"word":"ab er t",
"freq":317}]},
"enstin",{
"numFound":10,
"startOffset":6,
"endOffset":12,
"origFreq":0,
"suggestion":[{
"word":"einstein",
"freq":92},
{
"word":"ens tin",
"freq":44},
{
"word":"enshrin",
"freq":13},
{
"word":"en s tin",
"freq":673},
{
"word":"enjoin",
"freq":12},
{
"word":"e ns tin",
"freq":335},
{
"word":"entwin",
"freq":8},
{
"word":"ens t in",
"freq":317},
{
"word":"epstein",
"freq":8},
{
"word":"en st in",
"freq":231}]},
"correctlySpelled",false,
"collation",[
"collationQuery","albert einstein",
"hits",154,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","einstein"]],
"collation",[
"collationQuery","(aber t) einstein",
"hits",375,
"misspellingsAndCorrections",[
"abert","aber t",
"enstin","einstein"]],
"collation",[
"collationQuery","albert (ens tin)",
"hits",335,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","ens tin"]],
"collation",[
"collationQuery","albert enshrin",
"hits",137,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","enshrin"]],
"collation",[
"collationQuery","alert einstein",
"hits",139,
"misspellingsAndCorrections",[
"abert","alert",
"enstin","einstein"]]]}}
我想獲得以下字段
"collation",[
"collationQuery","albert einstein",
"hits",154,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","einstein"]],
特別是'阿爾伯特愛因斯坦'。 我嘗試了以下代碼,但收到錯誤。
$myArray = json_decode($response, true);
foreach ($myArray['collation'] as $doc) {
echo $doc[0];
}
這里有很多問題。
responseHeader
, response
, spellcheck
是根級字段。 其他一切都嵌套在它們之下。 print_r($myArray)
執行json_decode
解析的內容。 $myArray
是一個對象,而不是一個數組。 像$myArray->response
一樣訪問對象,而不是$myArray['response']
。 你所謂的“一個領域”不是一個領域。 json_encode
將該數據的結構視為:
[6] => collation [7] => Array ( [0] => collationQuery [1] => albert einstein [2] => hits [3] => 154 [4] => misspellingsAndCorrections [5] => Array ( [0] => abert [1] => albert [2] => enstin [3] => einstein ) )
如果這是您輸出JSON的代碼,那么您將需要更好地構建它。 如果這是別人的代碼,那就是他們的意思。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.