【问题标题】:What is the correct method to output list contents using JSON?使用 JSON 输出列表内容的正确方法是什么?
【发布时间】:2013-07-20 20:49:02
【问题描述】:

我正在使用 yelp API 来为我的网站随机抽取附近的列表。来自 yelp 在其网站上的文档:

rating                 number   Rating for this business (value ranges from 1, 1.5, ... 4.5, 5)
rating_img_url         string   URL to star rating image for this business (size = 84x17)
rating_img_url_small   string   URL to small version of rating image for this business (size = 50x10)
rating_img_url_large   string   URL to large version of rating image for this business (size = 166x30)
snippet_text           string   Snippet text associated with this business
snippet_image_url      string   URL of snippet image associated with this business
location               dict     Location data for this business
location.address       list     Address for this business. Only includes address fields.
location.display_address list   Address for this business formatted for display. Includes all address fields, cross streets and city, state_code, etc.
location.city          string   City for this business
location.state_code    string   ISO 3166-2 state code for this business
location.postal_code   string   Postal code for this business
location.country_code  string   ISO 3166-1 country code for this business
location.cross_streets string   Cross streets for this business

如何输出 location.display_address 等位置变量?我下面的代码通过回显“$response->businesses->rating”正确输出字符串和数字。但是,我的代码的最后一行不起作用。

// Handle Yelp response data
$response = json_decode($data);
$business = $response->businesses;

$numbers = range(0, 19);
shuffle($numbers);
echo "<img src='".$business[$numbers[$ran]]->image_url."'><br/>";
echo $business[$numbers[$ran]]->name."<br/>";
echo "<img border=0 src='".$business[$numbers[$ran]]->rating_img_url_large."'><br/>";
echo "<br/>";

echo $business[$numbers[$ran]]->location[display_address]."<br/>";

我得到的错误是

“致命错误:无法在 /home/content/38/11397138/html/yelpcall.php 第 51 行使用 stdClass 类型的对象作为数组”

编辑: var dump 返回:

object(stdClass)#14 (6) { ["city"]=> string(11) "Chino Hills" ["display_address"]=> array(2) { [0]=> string(14) "2923 Chino Ave" [1]=> string(21) "Chino Hills, CA 91709" } ["postal_code"]=> string(5) "91709" ["country_code"]=> string(2) "US" ["address"]=> array(1) { [0]=> string(14) "2923 Chino Ave" } ["state_code"]=> string(2) "CA" }

【问题讨论】:

  • 不工作 对问题的描述不是很清楚......因为它是list,您可能应该参考它的单个元素。尝试var_dump($business[$numbers[$ran]]-&gt;location[display_address]) 或阅读文档。
  • @Eggplant 对不起哈哈。我添加了从最后一行代码中得到的错误。 var 转储告诉我显示地址是两个字符串的数组,但我仍然不确定如何输出内容

标签: php json api yelp


【解决方案1】:

试试var_dump($business[$numbers[$ran]]-&gt;location);,这会告诉你更多关于你正在使用的变量的信息,并帮助你公开它的属性。

在您的情况下,位置属性的类型似乎是 StdClass。因此访问属性如下:$business[$numbers[$ran]]-&gt;location-&gt;display_address

编辑:感谢您的更新。 StdClass 的 display_address 属性是一个数字索引数组,因此要访问数组的元素,您可以这样做: $business[$numbers[$ran]]-&gt;location-&gt;display_address[0] 但它看起来更像是这样使用的:

$address = $business[$numbers[$ran]]->location->display_address;
foreach( $address as $line ){
    echo $line.'<br />';
} 

【讨论】:

  • 我将 vardump 添加到我的编辑中。我试过: echo $business[$numbers[$ran]]->location->display_address;输出是“数组”
  • 谢谢先生,您的解释和工作解决方案。这是题外话,但是当我这样做时: echo "image_url."'>
    ";它输出一个 100x100 的图像。当我尝试在 img 标签中添加 width="200" height="200" 时出现错误是否有原因?由于某种原因,我无法更改 image_url 的大小
  • 几乎可以肯定不是图像大小调整而是源路径。尝试仔细检查 image_url。
【解决方案2】:

尝试 echo $business[$numbers[$ran]]->location["display_address"]."
"; (在“display_address”键名周围添加引号。

【讨论】:

  • echo $business[$numbers[$ran]]->location["display_address"];不适合我
猜你喜欢
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多