我正在学习d3.d3 js中有certain ways of loading the data个.但他们似乎都做出了一个决定.在我的场景中,我已经有了字符串中的json数据.如何使用此字符串而不是发出另一个http请求?我试图寻找这方面的文档,但没有找到.

This works:

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

Now, if I have:

//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 }]}]}]}'; 

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

推荐答案

Simply replace d3.json call with

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

UPDATE 09/2013

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

Json相关问答推荐

从json数组中删除特定元素

修改Deneb图表中工具提示的字体大小

VSCode 为 python 文件添加标尺,但不为 C 文件添加标尺.为什么?

Rust实现:高效解析任意大小的JSON数组

嵌套存储在字符串变量中的 JSON 对象

Scala - 在构建 Json 时无法删除 Key -> value "{}" 大括号的双引号

如何从 JSON 中获取数据并通过 vb6 在网格中显示

Powershell 无法从名为 count 的键中获取价值

使用 Ansible 解析来自 Juniper 交换机的 JSON 输出

杰克逊 2.0 和 Spring 3.1

可以通过 POST 使用 EventSource 传递参数的服务器发送事件 (SSE)

如何使用 CORS 实现 JavaScript Google Places API 请求

JSON RPC - 什么是id?

应该使用什么标头将 GZIP 压缩 JSON 从 Android 客户端发送到服务器?

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

Rails 中奇怪的 JSON Javascript 问题

将 PHP 结果数组转换为 JSON

如何从 MVC 控制器返回 Json 对象以查看

ASP.NET Web API JSON 输出中没有时间的日期

有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?