【问题标题】:Is this a JSON array?这是一个 JSON 数组吗?
【发布时间】:2014-01-26 20:25:03
【问题描述】:

这是一个 JSON 数组吗:http://flamencopeko.net/icons_ajax.php

来源:http://flamencopeko.net/icons_ajax.txt

$urls = array(); foreach (glob(dirname(__FILE__) . '/ico/*.gif') as $file) { $urls[] = 'http://flamencopeko.net/ico/' . basename($file); };

$index = 0;

if (isset($_GET['index'])) {
    $index = (int) $_GET['index'];

    if ($index > (count($urls) - 1)) {
        // Out of bounds, reset
        $index = 0;
    }
}

$previous = $index - 1;
$next = $index + 1;

if ($previous < 0) {
    $previous = count($urls) - 1;
}

if ($next == count($urls)) {
    $next = 0;
}

echo json_encode(array(
    'total' => count($urls),
    'url'   => $urls[$index],
    'next'  => $next,
    'prev'  => $previous,
    'alls' => $urls
));

我正在尝试将它与 php 一起使用。适用于 JavaScript。我已经尝试过 json_decode() 等等,但还没有输出。

【问题讨论】:

  • json_decode() 是否输出任何错误?是的,这在我看来就像 JSON。
  • 我不太确定你的意思,但该链接似乎非常有效的 JSON
  • 不,它不是 JSON 数组,您要查找的数组可能是 alls 属性。
  • 它不是一个数组。一个属性是一个数组。
  • 您可以使用jsonlint.com 进行 JSON 验证

标签: javascript php arrays json


【解决方案1】:

您展示的是一个 JSON 对象。它内部包含一个数组,但整个事物是一个对象。这样想——JSON可以是键值数据,使其成为对象,也可以是数组——单值,没有键之类的

[1,2,3,"some string",34,"another string"]

您展示的是一个完全有效的 JSON 对象。它是否适合你的需要——我们不能说。您可以查看json.org 了解确切的规范以及什么是有效的 JSON。

此外,任何传递给json_encode() 的数组都将输出一个有效的 JSON。

【讨论】:

  • echo (json_decode('icons_ajax.php?alls', true)); 什么也不输出。然后我的代码或多或少是猜测。非常欢迎关于如何转换为 php 数组的建议。我应该从flamencopeko.net/icons_ajax.php 传递 header() 吗?我试过了,但没有帮助。
  • 当然不会。 json_decode() 不会打开流。您需要做的是首先获取内容。换句话说,类似于var_dump(json_decode(file_get_contents('icons_ajax.php?alls'), true));。 Echo 会输出Array() 所以你需要使用var_dump() 或者print_r()
  • 啊。我们应该同时使用json_decodefile_get_contents。很酷。现在我至少得到一个错误:警告:file_get_contents(icons_ajax.php?alls) [function.file-get-contents]: failed to open stream: No such file or directory in /home/flamenco/public_html/icons_cogo_19.php在第 183 行 NULL
  • 可能是因为它在 JSON 完全加载之前执行。我的菜鸟代码需要很长时间。
  • 有什么办法让 PHP 等到 JSON 被加载?环顾四周,一无所获。
【解决方案2】:

我喜欢使用http://jsonviewer.stack.hu/ 的在线 json 查看器 - 这表明您有一个有效的 json 对象;您可以使用它来查看它由一些标题信息和“alls”数组中的 406 个图像组成的数组。

【讨论】:

    猜你喜欢
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    相关资源
    最近更新 更多