【问题标题】:JSON output from PHP using d3.json使用 d3.json 从 PHP 输出 JSON
【发布时间】:2013-05-06 21:45:20
【问题描述】:

我创建了一个 PHP 文件来查询 JSON 输出。特定过滤器“testPHP.php?number=123”的 PHP 文件的 JSON 输出是

[{"source":"AB","target":"AC","type":"true"},{"source":"UB","target":"EP","type":"true"},{"source":"US","target":"UR","type":"lie"},{"source":"BS","target":"QW","type":"lie"},{"source":"UW","target":"EA","type":"lie"}]

我已经在 html 文件中尝试过将 JSON 输出读取到链接变量

var links; // a global
d3.json("testPHP.php?number=123", function(error, json) {
  if (error) return console.warn(error);
  links = json;
});

但是,数据似乎没有存储在链接中。 如何将数据存储在var links

基本上,我想将 https://gist.github.com/mbostock/1153292 中的 var links 替换为 PHP 的 JSON 输出。

编辑: 还是http://localhost:8080 导致了问题?

【问题讨论】:

  • 您的 JSON 无效。你是如何产生这个 JSON 的?数组中的最后一个对象缺少引号。
  • 拼写错误。现在改了。

标签: php javascript json d3.js


【解决方案1】:

d3.json 调用是异步的;在填充“链接”变量之前执行回调后包含的代码。

var links; // a global
d3.json("testPHP.php?number=123", function(error, json) {
  if (error) return console.warn(error);
  links = json;

  console.log(links); // your links are populated here

  // include the rest of your app here
});

// links is still undefined here because the success callback hasn't been run yet

【讨论】:

    【解决方案2】:

    你需要写links = [{"source":"AB","target":"AC","type":"true"},{"source":"UB","target":"EP","type":"true"},{"source":"US","target":"UR","type":"lie"},{"source":"BS","target":"QW","type":"lie"},{"source":"UW,"target":"EA","type":"lie"}]

    此时链接将保存该信息。但是,您可能需要考虑一个数组。

    【讨论】:

    • 我需要使用php文件的json输出!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2014-04-14
    • 2013-07-31
    • 1970-01-01
    相关资源
    最近更新 更多