d3 js - 在没有 http get 的情况下加载 json [英] d3 js - loading json without a http get

查看:23
本文介绍了d3 js - 在没有 http get 的情况下加载 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 d3.在 d3 js 中有加载数据的某些方式.但他们似乎都在做一个 HTTP GET.在我的场景中,我已经有一个字符串中的 json 数据.如何使用此字符串而不是发出另一个 http 请求?我试图为此寻找文档,但没有找到.

I am learning d3. There are certain ways of loading the data in d3 js. But all of them seem to make a HTTP GET. In my scenario, I already have the json data in a string. How can I use this string instead of making another http request? I tried to look for documentation for this but found none.

这有效:

d3.json("/path/flare.json", function(json) {
    //rendering logic here
}

现在,如果我有:

//assume this json comes from a server (on SAME DOMAIN)
var myjson = '{"name": "flare","children": [{"name": "analytics","children": [{"name": "cluster","children": [{"name": "MergeEdge", "size": 10 }]}]}]}'; 

如何在 d3 & 中使用已计算的myjson"避免对服务器的异步调用?谢谢.

How do I use already computed 'myjson' in d3 & avoid a async call to server? Thanks.

推荐答案

只需将 d3.json 调用替换为

json = JSON.parse( myjson );

浏览器:

var myjson = '{"name": "flare","children": [{"name": "analytics","children": [{"name": "cluster","children": [{"name": "MergeEdge", "size": 10 }]}]}]}';

// d3.json("/path/flare.json", function(json) { #delete this line

    json = JSON.parse( myjson ); //add this line

    //rendering logic here

//} #delete this line

更新 09/2013

原始代码已更改.所以 varname json 应该是 root:

Original code has changed. So varname json should be root:

// d3.json("flare.json", function(error, root) { #delete this line

    root = JSON.parse( myjson ); //add this line

    //rendering logic here

//} #delete this line

这篇关于d3 js - 在没有 http get 的情况下加载 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆