我可以使用d3.json处理类似json格式的字符串吗? [英] Can I process a string which is like a json format using d3.json?

查看:66
本文介绍了我可以使用d3.json处理类似json格式的字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线搜索,而大多数人直接从网址或本地文件夹中读取.json文件

I've searched online while most people are directly reading the .json file from url or local folder like

 console.log("before csv ");
 d3.csv("cities.csv", function(data) {console.log(data)});
 console.log("before json");
 d3.json("flare.json",function(error,data2) {console.log(error, data2)});

虽然目前我的要求是我有一个字符串,它是json格式,如

while currently my requirement is that I have a string, which is in a json format, like

{"nodes": [
    {"id": "Myriel"},
    {"id": "Napoleon"},
    {"id": "Mlle.Baptistine"},
    {"id": "Mme.Magloire"},
    {"id": "CountessdeLo"},
    {"id": "Geborand"},
    {"id": "Champtercier"},
    {"id": "Cravatte"},
    {"id": "Count"},
    {"id": "OldMan"},
    {"id": "Labarre"},
    {"id": "Valjean"},
    {"id": "Marguerite"},
    {"id": "Mme.deR"}
]}

有没有一种方法我可以直接处理这个字符串作为从.json或.csv文件读取的数据?

is there a method I could directly process this string as the data read from .json or .csv file?

推荐答案

D3仅适用于JS对象,d3.json只是一种从外部文件加载对象的方法。因此,如果您不需要从外部文件加载数据,则不要使用d3.json。

D3 works with the JS objects only, d3.json is just a method to load the object from an external file. So if you dont need to load data from external file then dont use d3.json.

您可以这样做的方式之一:

One of the way you can do like:

 var jsonObj = JSON.parse(data);

这篇关于我可以使用d3.json处理类似json格式的字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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