我有以下JSON,我需要从中提取PHP中的图标的URL:
product
id 3
name "ETC Source 4 19deg"
type "Product"
tax_class_id 2
rental_revenue_group_id 1
sale_revenue_group_id 2
created_at "2017-10-02T10:50:32.239Z"
updated_at "2017-10-02T16:48:44.844Z"
custom_fields {}
product_group {…}
tax_class {…}
icon
id 1
iconable_id 3
iconable_type "Item"
image_file_name "Source_4_fixed.jpg"
url "https://s3.amazonaws.com/current-rms/899701e0-898a-0135-ef0d-0a9ca217e95b/icons/1/original/Source_4_fixed.jpg"
thumb_url "https://s3.amazonaws.com/current-rms/899701e0-898a-0135-ef0d-0a9ca217e95b/icons/1/thumb/Source_4_fixed.jpg"
created_at "2017-10-02T16:48:44.747Z"
updated_at "2017-10-02T16:48:44.747Z"
我使用以下内容获取顶级信息:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
return $data;
public function getProduct( $id )
{
$data = $this->query('products/' . $id);
return $data->product;
}
$html .= '<div class="row">';
$html .= 'Product Name is:' . $product->name;
$html .= '</div>';
echo $html;
任何帮助赞赏。
克里斯
您在问题中发布的内容不是JSON格式,但它看起来像条纹格式。但是,要访问一个元素,你只需要像树一样穿过它,在你的情况下,你想要URL
,所以它变成:
product -> icon -> url
和PHP版本:
echo $data->icon->url;
您还可以将JSON格式转换为数组,然后访问以下元素:
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, TRUE); // passing true parameter
return $data;
}
echo $data['product']['icon']['url'];
有关更多解释和示例,请阅读此问题'qazxsw poi'的答案。