json to php array cdnjs


json to php array cdnjs

我正在尝试从此链接将cdnjs api JSON结果转换为php数组。这是我的代码:

<pre>
<?php
    $cdnLinks = file_get_contents('http://api.cdnjs.com/libraries');
    $cdnLinks = json_encode($cdnLinks);
    $j = json_decode($cdnLinks);
    print_r($j);
?>
</pre>

我错过了什么?谢谢

这是你正在做的:

  1. 下载一串 JSON
  2. 将该字符串编码为 JSON(您现在有一个包含 JSON 对象的 JSON 字符串)
  3. 将该 JSON 解码回一串 JSON
  4. 打印字符串

您需要跳过步骤 2。

跳过步骤 2,因为输出已经是 json 字符串。要将 json 解码为数组,您还需要使用第二个参数作为 true json_decode($jsonObj, true)。

谢谢