[英]How to output JSON array in HTML
我将ECWID用于我的网站商店,并且想要创建一些代码,为此我必须执行一些JSON请求,而我从未使用过JSON。 当我在地址栏中输入以下内容时: http ://app.ecwid.com/api/v1/STOREID/product ? id=当然是PRODUCTID,当然是用引用我的商店和特定产品的编号替换storeid和productid的消息归还
{
"id": 8443685,
"sku": "KEN000025",
"smallThumbnailUrl": "picture URL here",
"thumbnailUrl": "thumbnail URL here",
"imageUrl": "image URL here",
"name": "Kenwood Excelon KFC-X173",
"price": 99.99,
"weight": 1.0,
"url": "long URL here",
"description": "long description here",
"options": [],
"taxes": [],
"galleryImages": [],
"categories": [
{
"id": 769355,
"thumbnailUrl": "url here",
"name": "Speakers",
"url": "url here",
"description": ""
},
{
"id": 1466304,
"parentId": 1466305,
"thumbnailUrl": "URL here",
"name": "Kenwood",
"url": "URL here",
"description": ""
}
],
"dateAdded": 1325037351
}
现在,如果我有一个HTML文档,并且我想输出一个项目的图片,那么在该价格以下,我将如何使用JSON链接中的信息进行处理。
PS我知道Java脚本HTML PHP XML和许多其他语言,但不是JSON谢谢
JSON是非常简单但功能强大的交换信息格式。
如果要处理获取的JSON字符串(例如存储在$json
变量中),则可以通过以下方式简单地进行操作:
$decoded_json = json_decode($json, true);
(删除第二个参数,如果要代替对象的关联数组,则删除false
),然后将其作为简单或多维数组进行处理。 要将其更改回字符串,只需执行$json = json_encode($decoded_json);
。
您可以在以下位置看到解码后的JSON字符串: http : //codepad.org/ZARAK3Er
要输出适合您情况的缩略图URL和价格,请使用以下命令(不要忘记适当地输出$thumbnailUrl
和$price
price-下面的代码段仅为它们分配值):
$decoded_json = json_decode($json, true);
$thumbnailUrl = $decoded_json['thumbnailUrl'];
$price = $decoded_json['price'];
在此处查看证明: http : //codepad.org/5BKi9sPp
要将外部JSON文件提取到$json
字符串中,请使用file_get_contents()
。 在您的情况下,它可能看起来像这样:
$url = 'http://app.ecwid.com/api/v1/STOREID/product?id=PRODUCTID';
$json = file_get_contents($url);
通过粘贴上面讨论的所有部分,可以实现您想要的:
$url = 'http://app.ecwid.com/api/v1/STOREID/product?id=PRODUCTID';
$json = file_get_contents($url);
$decoded_json = json_decode($json, true);
$thumbnailUrl = $decoded_json['thumbnailUrl'];
$price = $decoded_json['price'];
结果,您将拥有$thumbnailUrl
和$price
变量以及所需的数据。 您也可以将其包含在一个函数中,该函数将使用商店ID和产品ID,并返回包含指定商店中指定产品的缩略图URL和价格的对象或关联数组。
对于递归方式,假设我想使用引导类输出为<table>
:
public static function jsonViewable($jsonText = '')
{
$arr = json_decode($jsonText, true);
$html = "";
if ($arr && is_array($arr)) {
$html .= self::_arrayToHtmlTableRecursive($arr);
}
return $html;
}
private static function _arrayToHtmlTableRecursive($arr)
{
$str = "<table class='table table-bordered'><tbody>";
foreach ($arr as $key => $val) {
$str .= "<tr>";
$str .= "<td>$key</td>";
$str .= "<td>";
if (is_array($val)) {
if (!empty($val)) {
$str .= self::_arrayToHtmlTableRecursive($val);
}
} else {
$str .= "<strong>$val</strong>";
}
$str .= "</td></tr>";
}
$str .= "</tbody></table>";
return $str;
}
现在,对myclass::jsonViewable($jsonString)
的调用将具有不错的输出。 请享用,
您可以使用PHP解码JSON,这会将其转换为关联数组,然后可以循环访问以显示数据:
您应该可以使用类似:
$productArray = json_decode( $JSON_DATA_VARIABLE, true );
$productImageOne = $productArray['categories'][0]['thumbnailUrl'];
$productImageTwo = $productArray['categories'][1]['thumbnailUrl'];
$price = $productArray['price'];
您必须先将JSON数据设置为变量,然后才能对其进行解码。 我确定该示例不是100%正确的(我不擅长在没有有效示例的情况下遍历数组),但是您明白了。 http://www.php.net/manual/en/function.json-decode.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.