簡體   English   中英

從PHP中的JSON_decode數組訪問值

[英]Accessing value from JSON_decode array in PHP

我正在嘗試從PHP中的JSON響應訪問值。

這是JSON輸出:

JSON回應

{"responseData": {"results":[{"GsearchResultClass":"GimageSearch","width":"1080","height":"720","imageId":"ANd9GcQWSDt_-TnzrNvEGEUoRSi56HfnP6GYXU5nDZYTH-jWw85xAZTOmtjjU5fq","tbWidth":"150","tbHeight":"100","unescapedUrl":"http://jblm-realestateguide.com/wp-content/gallery/fort-lewis-housing-areas_1/tacoma.jpg","url":"http://jblm-realestateguide.com/wp-content/gallery/fort-lewis-housing-areas_1/tacoma.jpg","visibleUrl":"jblm-realestateguide.com","title":"Cities Around Joint Base Lewis McChord, \u003cb\u003eWashington\u003c/b\u003e | Joint Base \u003cb\u003e...\u003c/b\u003e","titleNoFormatting":"Cities Around Joint Base Lewis McChord, Washington | Joint Base ...","originalContextUrl":"http://jblm-realestateguide.com/cities-around-jblm/","content":"\u003cb\u003eTacoma\u003c/b\u003e, \u003cb\u003eWA\u003c/b\u003e","contentNoFormatting":"Tacoma, WA","tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcQWSDt_-TnzrNvEGEUoRSi56HfnP6GYXU5nDZYTH-jWw85xAZTOmtjjU5fq"},{"GsearchResultClass":"GimageSearch","width":"441","height":"441","imageId":"ANd9GcRTxtYjg8d_R9V_fcu9WT2oThkLEzSOBrtnD5yh7sDv3EQ0mc1Ba8GQ4Z4","tbWidth":"127","tbHeight":"127","unescapedUrl":"http://washingtonhottubspacovers.com/imgs/tacoma-wa.jpg","url":"http://washingtonhottubspacovers.com/imgs/tacoma-wa.jpg","visibleUrl":"washingtonhottubspacovers.com","title":"\u003cb\u003eTacoma Washington\u003c/b\u003e Replacement Hot Tub Covers","titleNoFormatting":"Tacoma Washington Replacement Hot Tub Covers","originalContextUrl":"http://washingtonhottubspacovers.com/tacoma-hot-tub-covers.html","content":"\u003cb\u003eTacoma Washington\u003c/b\u003e Replacement","contentNoFormatting":"Tacoma Washington Replacement","tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcRTxtYjg8d_R9V_fcu9WT2oThkLEzSOBrtnD5yh7sDv3EQ0mc1Ba8GQ4Z4"},{"GsearchResultClass":"GimageSearch","width":"800","height":"534","imageId":"ANd9GcTLGrihrETQp8EcOYZdoJtsFRRHOuMEUCQZ3I8fUrhJJw4JX1xOAzvC3Qg","tbWidth":"150","tbHeight":"100","unescapedUrl":"https://lh5.googleusercontent.com/-79aR6CKFGcg/Tc2eit4NyjI/AAAAAAAAAAk/D1L0QcZ4-Aw/w800-h800/Rainier84_mount_rainier_and_tacoma_08-20-84.jpg","url":"https://lh5.googleusercontent.com/-79aR6CKFGcg/Tc2eit4NyjI/AAAAAAAAAAk/D1L0QcZ4-Aw/w800-h800/Rainier84_mount_rainier_and_tacoma_08-20-84.jpg","visibleUrl":"plus.google.com","title":"Ken Williams,CHFC CLTC CLU - Google+","titleNoFormatting":"Ken Williams,CHFC CLTC CLU - Google+","originalContextUrl":"https://plus.google.com/113866325996254808746","content":"Scrapbook photo 3","contentNoFormatting":"Scrapbook photo 3","tbUrl":"http://t0.gstatic.com/images?q\u003dtbn:ANd9GcTLGrihrETQp8EcOYZdoJtsFRRHOuMEUCQZ3I8fUrhJJw4JX1xOAzvC3Qg"},{"GsearchResultClass":"GimageSearch","width":"350","height":"238","imageId":"ANd9GcSmbeFTC2odTglWyJ2hjAuly1HJPNGPNvr2g4BsYy9haSF0aPHg4JN4YxU","tbWidth":"120","tbHeight":"82","unescapedUrl":"http://www.come2tacoma.com/images/tacoma_rainer.jpg","url":"http://www.come2tacoma.com/images/tacoma_rainer.jpg","visibleUrl":"www.come2tacoma.com","title":"\u003cb\u003etacoma\u003c/b\u003e_rainer.jpg","titleNoFormatting":"tacoma_rainer.jpg","originalContextUrl":"http://www.come2tacoma.com/","content":"County \u003cb\u003eWashington\u003c/b\u003e. \u003cb\u003eTacoma\u003c/b\u003e","contentNoFormatting":"County Washington. Tacoma","tbUrl":"http://t1.gstatic.com/images?q\u003dtbn:ANd9GcSmbeFTC2odTglWyJ2hjAuly1HJPNGPNvr2g4BsYy9haSF0aPHg4JN4YxU"}],"cursor":{"resultCount":"5,250,000","pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"5250000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026imgsz\u003dsmall%7Cmedium%7Clarge%7Cxlarge\u0026imgtype\u003dphoto\u0026hl\u003den\u0026q\u003dtacoma,wa","searchResultTime":"0.17"}}, "responseDetails": null, "responseStatus": 200}

PHP代碼:

$get_location_info = json_decode(file_get_contents($json_url),true);        
$location_image =  $get_location_info->responseData->results[0]->{'unescapedUrl'};

我正在嘗試從JSON響應中訪問“ unescapedUrl”值,並將其捕獲到PHP中。 我目前在上述代碼中獲得$ location_image的NULL。

任何想法我在上面做錯了嗎? 謝謝!

嘗試這個

$get_location_info['responseData']['results'][0]['unescapedUrl']

如果您不使用“ true”條件,則將類似於以下內容

$get_location_info->responseData->results[0]->unescapedUrl

請記住,此數據結構的“結果”位是一個數組,因此您應該遍歷它以獲取所有“ unsescapedUrl”值。

json_decode返回一個Array

要訪問unescapedUrl,您需要嘗試這樣的操作

$location_image = $get_location_info['responseData']['results'][0]['unescapedUrl'];

您還可以使用

foreach($get_location_info['responseData']['results'] as $result){

    echo $result['unescapedUrl'];
}

您正在json_decode中傳遞第二個參數,$ assoc = true。 這將返回一個關聯數組,而不是所使用符號的對象。

將此行更新為:

$get_location_info = json_decode(file_get_contents($json_url));

沒有真正的參數,您的對象表示法將起作用。 如果不這樣做,tf.alves建議

$location_image =  $get_location_info->responseData->results[0]->unescapedUrl;

由於最終要將JSON轉換為PHP數組,因此需要使用以下語法:

$count = count($get_location_info['responseData']['results']);
for($i = 0; $i < $count; $i++)
     echo $get_location_info['responseData']['results'][$i]['unescapedUrl'];

使用數組或對象。

暫無
暫無

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

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