从PHP中的JSON_decode数组访问值

时间:2013-12-31 21:51:25

标签: javascript php arrays json

我正在尝试从PHP中的JSON响应中访问一个值。

这是JSON输出:

JSON Response

{"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响应中访问“unes​​capedUrl”值并在PHP中捕获它。我目前在上面的代码中为$ location_image获取了一个NULL。

知道我上面做错了什么吗?谢谢!

4 个答案:

答案 0 :(得分:2)

试试这个

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

如果您没有使用“真实”条件,那就是这样的

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

请记住,此数据结构的“结果”位是一个数组,因此您应该遍历它以获取所有“unsescapedUrl”值。

答案 1 :(得分:2)

json_decode返回Array

要访问unescapedUrl,您需要尝试类似这样的内容

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

您也可以使用

迭代结果
foreach($get_location_info['responseData']['results'] as $result){

    echo $result['unescapedUrl'];
}

答案 2 :(得分:0)

您正在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;

答案 3 :(得分:0)

由于您最终将JSON转换为PHP数组,因此需要使用以下语法:

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

使用数组或对象。