【问题标题】:Can I process a string which is like a json format using d3.json?我可以使用 d3.json 处理类似于 json 格式的字符串吗?
【发布时间】:2018-06-28 05:10:32
【问题描述】:

我在网上搜索过,而大多数人都是直接从 url 或本地文件夹中读取 .json 文件 喜欢

 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 格式的字符串,比如

{"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 文件中读取的数据?

【问题讨论】:

    标签: javascript json d3.js


    【解决方案1】:

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

    你可以这样做的一种方式:

     var jsonObj = JSON.parse(data);
    

    【讨论】:

      【解决方案2】:

      你可以直接使用JSON.parse函数将字符串解析为JSON,而无需像这样使用d3-

      var json = JSON.parse(yourString);
      

      【讨论】:

        猜你喜欢
        • 2013-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多