我将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感谢
如何在PHP中使用JSON
JSON是一种非常简单但功能强大的信息交换格式。
如果您想处理您获取的JSON字符串(并存储在例如$json
变量中),只需执行以下操作:
$decoded_json = json_decode($json, true);
(如果需要对象而不是关联数组,请删除第二个参数或用false
替换),然后将其处理为简单或多维数组。要将其改回字符串,只需执行$json = json_encode($decoded_json);
即可。
您的解码JSON字符串可以在这里看到:http://codepad.org/ZARAK3Er
从JSON获取值
要在您的情况下输出缩略图URL和价格,只需使用以下内容(不要忘记适当地输出$thumbnailUrl
和$price
——下面的代码片段只是为它们赋值):
$decoded_json = json_decode($json, true);
$thumbnailUrl = $decoded_json['thumbnailUrl'];
$price = $decoded_json['price'];
请在此处查看证明:http://codepad.org/5BKi9sPp
正在获取外部JSON文件
要将外部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