我坚持检索下面的json响应是json输出。非常感谢您的帮助。
{ "productHeader" : { "totalHits" : 684 }, "products" : [ { "name" : "Victoria Hotels", "productImage" : { "url" : "http://hotels.com/hotels/9000000/8640000/8633700/8633672/8633672_20_b.jpg" }, "language" : "en", "description" : "Location. Victoria Hotels is in Foshan (Nanhai) and area attractions include Renshou Temple and New Plaza Stadium. Additional regional attractions include Guangdong Folk Art Museum and Bright Filial Piety Temple.", "identifiers" : { }, "fields" : [ { "name" : "regions2", "value" : "Guangdong" },
请帮我提取特定值。例如,如果我需要获取名称,请从json响应中获取图像URL。
答案 0 :(得分:1)
您可以使用json_decode将JSON字符串解析为数组并访问它的值:
// assuming, that $string contains the json response
// second parameter to true, to get an array instead of an object
$data = json_decode( $string, true );
if ( $data ) {
echo $data['products'][0]['name'];
// or whatever value
} else {
echo 'JSON could not be parsed, error: ' . json_last_error();
}
要显示products数组中的所有值,请简单循环:
if ( $data ) {
foreach($data['products'] as $product){
echo $product['name'];
}
// or whatever value
} else {...
答案 1 :(得分:0)
如果您要使用PHP获取值,请查看:
$decodedJson = json_decode($json, true);
print_r($decodedJson);
您将获得一个数组,以获取在循环中执行此操作所需的图像源和其他标记,或选择具体的索引,如:
echo $decodedJson['productHeader']['products'][0]['productImage']['url'];
在JS中,您应该以相同的方式提取值。