简体   繁体   English

d3.json()和JSON.parse()会产生相同的结果吗?

[英]Does d3.json() and JSON.parse() produce the same results?

I am having an issue moving from loading an external JSON file to using a local json string. 从加载外部JSON文件到使用本地json字符串,我遇到了问题。 At first, it seemed like d3.json() returned the same JSON array that I got using JSON.parse() inside of a $.getJSON(), although, while they seem similar, it looks like d3.json added an "ID" variable to my JSON data. 最初,似乎d3.json()返回了我在$ .getJSON()内使用JSON.parse()获得的同一JSON数组,尽管虽然它们看起来很相似,但看起来d3.json却添加了一个“ ID”变量添加到我的JSON数据中。 Which seems pretty important when constructing a Sankey Graph in D3 . 在D3中构造Sankey图时,这似乎非常重要。 Also, using the exact same data, with $.getJSON() and JSON.parse(), this ID element is not present in the exact same output calls. 同样,使用完全相同的数据,以及$ .getJSON()和JSON.parse(),此ID元素也不存在于完全相同的输出调用中。 Does D3 purposely add an "ID" element to its data? D3是否故意在其数据中添加“ ID”元素? How do I achieve the same result with JSON.parse()? 如何使用JSON.parse()获得相同的结果?

So visually, the output of a console.log(mygraph) call appears like: 因此,从视觉上看,console.log(mygraph)调用的输出看起来像:

    Object {source: Object, target: Object, value: "0", path: "path#0", dy: 0}
    dy:0
    id:0
    path:"path#3"
    source:Object
    sy:0
    target:Object
    ty:0
    value:"0"
    __proto__:Object

But, the latter method with a console.log(mygraph) call appears like: 但是,带有console.log(mygraph)调用的后一种方法看起来像:

    Object {source: Object, target: Object, value: "0", path: "path#0", dy: 0}
    dy:0
    path:"path#3"
    source:Object
    sy:0
    target:Object
    ty:0
    value:"0"
    __proto__:Object

What exactly is this "ID" and how do I get it using the latter method? 这个“ ID”究竟是什么?如何使用后一种方法来获取它? Actually, how do I load a local JSON variable into D3? 实际上,如何将本地JSON变量加载到D3中?

d3.json() is like $.getJSON() , in that it takes a url string as the 1st param, and a function called when the data is loaded as 2nd param. d3.json()类似于$.getJSON() ,因为它以url字符串作为第一参数,并且当数据作为第二参数加载时调用了一个函数。

JSON.parse() does something else. JSON.parse()还有其他功能。 It takes a string that looks like JSON and parses it into javascript objects like json. 它采用类似于JSON的字符串并将其解析为JSON之类的javascript对象。

Neither one will add anything such as an id param to the data it produces. 任何人都不会在其生成的数据中添加任何东西,例如id参数。 id would have to be in the data you're loading/parsing. id必须在您正在加载/解析的数据中。

One thing that might have happened to produce id is that you successfully loaded/parsed your data, then passed the it to another function that added the id prop. 可能会产生id一件事是您成功加载/解析了数据,然后将其传递给添加了id属性的另一个函数。 In that case, it's even possible that some of your confusion occurred from the way Chrome's console.log lets you expand whatever object you logged. 在这种情况下,甚至有可能您的某些困惑是由于Chrome的console.log允许您扩展记录的任何对象的方式。 The thing is that even if you console.log 'ed your object BEFORE some other function added the id prop to it, BY THE TIME you clicked to expand it, it was already there and showed up. 事实是,即使在您向对象添加console.log之前,在其他功能添加了id属性之前,当您单击以展开它时,该对象已经存在并显示出来了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM