【问题标题】:How to output JSON array in HTML如何在 HTML 中输出 JSON 数组
【发布时间】:2012-03-21 20:53:19
【问题描述】:

我在我的网站商店中使用 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 谢谢

【问题讨论】:

  • 那么,你想用javascript来做吗? php?
  • javascript 会工作感谢您的回复
  • 有没有适合您的解决方案?

标签: php javascript html json


【解决方案1】:

如何在 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 - 下面的 sn-p 只是为它们分配值):

$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 和价格的对象或关联数组。

【讨论】:

  • 好的,现在说得通了,所以我每次都必须将 JSON 文本实际粘贴到代码中,还是有办法直接从 URL 中提取数据?
  • @Timmer1992:是的,当然有办法获取 JSON 并将其分配给 $json 变量。你用什么 PHP 框架?
  • @Timmer1992:如果您使用 PHP 框架,请参阅其文档 - 应该(可能)有一些帮助程序来执行外部请求。在原始 PHP 中,您可以使用例如。 file_get_contents() function 就像这样:$json = file_get_contents($url);(其中 $url 是包含您要获取的 URL 的字符串)。
【解决方案2】:

对于递归方式,假设我想使用引导类输出为<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) 的调用将有一个不错的输出。 享受吧,

【讨论】:

    【解决方案3】:

    您可以使用 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

    【讨论】:

    • 那么我如何将它链接到 JSON url?因此,如果我重写该数据并根据项目将不同的 URL 传递给它,它仍然会输出一个盒子,其中包含该项目的图片以及低于该价格的价格。我不太明白如何应用它来从我的 JSON URL 输出代码
    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2015-10-02
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多