Json php 正在解析 zomato
Json php parsing zomato
我正在使用 zomato api 来获得一个 json 输出,它通过 php 工作但是我想从我得到的所有数据中打印出几个数组我对此有困难。请有人帮忙。这是我得到的输出
Array
(
[R] => Array
(
[res_id] => 18559961
)
[apikey] => 123
[id] => 18559961
[name] => Between Buns
[url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
[location] => Array
(
[address] => C13, SDA Market, Opposite IIT Delhi, SDA, New Delhi
[locality] => SDA Market, SDA
[city] => New Delhi
[city_id] => 1
[latitude] => 28.5465059222
[longitude] => 77.1969081089
[zipcode] =>
[country_id] => 1
[locality_verbose] => SDA Market, SDA, New Delhi
)
[switch_to_order_menu] => 0
[cuisines] => Burger, American, Desserts, Sandwich, Salad, European
[average_cost_for_two] => 1500
[price_range] => 3
[currency] => Rs.
[offers] => Array
(
)
[opentable_support] => 0
[is_zomato_book_res] => 0
[mezzo_provider] => OTHER
[is_book_form_web_view] => 0
[book_form_web_view_url] =>
[book_again_url] =>
[thumb] => https://b.zmtcdn.com/data/pictures/chains/1/18559961/c0e491e63d2e95c0c3049de0e8bbb05c.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A
[user_rating] => Array
(
[aggregate_rating] => 4.0
[rating_text] => Very Good
[rating_color] => 5BA829
[votes] => 723
)
[photos_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop
[menu_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop
[featured_image] => https://b.zmtcdn.com/data/pictures/chains/1/18559961/c0e491e63d2e95c0c3049de0e8bbb05c.jpg?output-format=webp
[medio_provider] => 1
[has_online_delivery] => 0
[is_delivering_now] => 0
[include_bogo_offers] => 1
[deeplink] => zomato://restaurant/18559961
[is_table_reservation_supported] => 1
[has_table_booking] => 1
[book_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/book?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
[events_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
)
这是我的代码
<?php
// Errors on
error_reporting(E_ALL);
// Get cURL resource
$curl = curl_init();
// Curl options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Accept: application/json', 'user-key: 123'],
CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/restaurant?res_id=123',
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Check for errors if curl_exec fails
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
// Close request to clear up some resources
curl_close($curl);
// dumping $resp
//echo 'var_dump: <br>';
//var_dump($resp);
//echo '<br><br>';
// Decode json
$jsonZomato = json_decode($resp,true);
// print readable results
print "<pre>";
print_r($jsonZomato);
print "</pre>";
print $jsonZomato->aggregate_rating;//this is the problem
?>
我得到的错误是注意:试图在第 45
行 /home/betweenbuns/public_html/rating_2.php 中获取非对象的 属性
当您在 json_decode()
函数中传递第二个参数 true
时,它是 returns 数组。
正如您在这一行所做的那样:
// Decode json
$jsonZomato = json_decode($resp,true);
因此,使用键作为数组访问它:
print $jsonZomato['user_rating']['aggregate_rating'];//This should work
我正在使用 zomato api 来获得一个 json 输出,它通过 php 工作但是我想从我得到的所有数据中打印出几个数组我对此有困难。请有人帮忙。这是我得到的输出
Array
(
[R] => Array
(
[res_id] => 18559961
)
[apikey] => 123
[id] => 18559961
[name] => Between Buns
[url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
[location] => Array
(
[address] => C13, SDA Market, Opposite IIT Delhi, SDA, New Delhi
[locality] => SDA Market, SDA
[city] => New Delhi
[city_id] => 1
[latitude] => 28.5465059222
[longitude] => 77.1969081089
[zipcode] =>
[country_id] => 1
[locality_verbose] => SDA Market, SDA, New Delhi
)
[switch_to_order_menu] => 0
[cuisines] => Burger, American, Desserts, Sandwich, Salad, European
[average_cost_for_two] => 1500
[price_range] => 3
[currency] => Rs.
[offers] => Array
(
)
[opentable_support] => 0
[is_zomato_book_res] => 0
[mezzo_provider] => OTHER
[is_book_form_web_view] => 0
[book_form_web_view_url] =>
[book_again_url] =>
[thumb] => https://b.zmtcdn.com/data/pictures/chains/1/18559961/c0e491e63d2e95c0c3049de0e8bbb05c.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A
[user_rating] => Array
(
[aggregate_rating] => 4.0
[rating_text] => Very Good
[rating_color] => 5BA829
[votes] => 723
)
[photos_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop
[menu_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop
[featured_image] => https://b.zmtcdn.com/data/pictures/chains/1/18559961/c0e491e63d2e95c0c3049de0e8bbb05c.jpg?output-format=webp
[medio_provider] => 1
[has_online_delivery] => 0
[is_delivering_now] => 0
[include_bogo_offers] => 1
[deeplink] => zomato://restaurant/18559961
[is_table_reservation_supported] => 1
[has_table_booking] => 1
[book_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/book?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
[events_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
)
这是我的代码
<?php
// Errors on
error_reporting(E_ALL);
// Get cURL resource
$curl = curl_init();
// Curl options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Accept: application/json', 'user-key: 123'],
CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/restaurant?res_id=123',
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Check for errors if curl_exec fails
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
// Close request to clear up some resources
curl_close($curl);
// dumping $resp
//echo 'var_dump: <br>';
//var_dump($resp);
//echo '<br><br>';
// Decode json
$jsonZomato = json_decode($resp,true);
// print readable results
print "<pre>";
print_r($jsonZomato);
print "</pre>";
print $jsonZomato->aggregate_rating;//this is the problem
?>
我得到的错误是注意:试图在第 45
行 /home/betweenbuns/public_html/rating_2.php 中获取非对象的 属性当您在 json_decode()
函数中传递第二个参数 true
时,它是 returns 数组。
正如您在这一行所做的那样:
// Decode json
$jsonZomato = json_decode($resp,true);
因此,使用键作为数组访问它:
print $jsonZomato['user_rating']['aggregate_rating'];//This should work