将JSON字符串转换为对象的问题

时间:2019-06-13 06:33:26

标签: php arrays json

拉出curl请求后,我得到的响应没有错误。我的问题是访问数组数据。

var_dump($ json)我得到JSON数据。我将数组数据设置为带有索引的$ variable,但仍然出现错误。

JSON数据

array(2) { ["meta"]=> array(1) { ["total_listings"]=> int(5994) } ["data"]=> array(1) { ["listings"]=> array(3) { [0]=> array(33) { ["id"]=> int(47603) ["name"]=> string(8) "The Crop" ["slug"]=> string(8) "the-crop" ["state"]=> string(2) "CA" ["city"]=> string(11) "Bakersfield" ["type"]=> string(10) "dispensary" ["wmid"]=> int(155018451) ["latitude"]=> float(35.414963) ["longitude"]=> float(-119.056766) ["web_url"]=> string(42) "https://weedmaps.com/dispensaries/the-crop" ["package_level"]=> string(12) "listing_plus" ["feature_order"]=> int(1) ["ranking"]=> float(4.9763046192414) ["rating"]=> float(4.9826867247295) ["reviews_count"]=> int(3789) ["avatar_image"]=> array(2) { ["small_url"]=> string(124) "https://images.weedmaps.com/dispensaries/000/047/603/avatar/square_fill/1546807008-B60BEF19-5F08-48E1-832E-B206B990BC96.jpeg" ["original_url"]=> string(121) "https://images.weedmaps.com/dispensaries/000/047/603/avatar/original/1546807008-B60BEF19-5F08-48E1-832E-B206B990BC96.jpeg" } ["license_type"]=> string(7) "medical" ["address"]=> string(13) "5950 State Rd" ["distance"]=> NULL ["zip_code"]=> string(5) "93308" ["timezone"]=> string(19) "America/Los_Angeles" ["intro_body"]=> string(352) "
We couldn't be more proud of our Crop Family for helping us achieve our goal of becoming Kern County's first state licensed medical cannabis dispensary. We could not have done this without our all star staff and incredibly loyal patients! We look forward to continuing to provide safe access to legal medicine for all of Kern County." ["static_map_url"]=> string(82) "https://staticmap.weedmaps.com/static_map/13/35.414963/-119.056766/402/147/map.png" ["open_now"]=> bool(false) ["closes_in"]=> NULL ["todays_hours_str"]=> string(6) "Closed" ["min_age"]=> int(18) ["menu_items_count"]=> int(113) ["verified_menu_items_count"]=> int(78) ["endorsement_badge_count"]=> int(36) ["is_published"]=> bool(true) ["online_ordering"]=> array(2) { ["enabled_for_pickup"]=> bool(false) ["enabled_for_delivery"]=> bool(false) } ["retailer_services"]=> array(1) { [0]=> string(10) "storefront" } }

我的代码

$json = json_decode($response, true);

var_dump($json);

foreach($json['listings'] as $dis){

$dispensary_name = $dis['name'];
//$dispensary_state = $dis['state'];
//$dispensary_city = $dis['city'];

echo "<p>". $dispensary_name ."</p>";
}

我希望从查看JSON数据的回声中显示“ The Crop ”。但是我当前收到此错误:为foreach()提供了无效的参数

0 个答案:

没有答案