简体   繁体   English

在D3 JavaScript中动态构建JSON

[英]Building JSON dynamically in D3 javascript

I am trying to build a JSON dynamically by reading from an array in D3-javascript. 我正在尝试通过从D3-javascript中读取数组来动态构建JSON。 (The below code is not the actual one I used, but similar to this) (以下代码不是我实际使用的代码,但是与此类似)

 radius = [10,20,30];
   jsonradius = '[{';
   for i = 0 to radius.length {
       jsonradius += '"radius":';  
       jsonradius += 'radius[i]';
       jsonradius += ',';
   }
   jsonradius += '}]';

using flags I make sure that the comma is not placed after the last entry and it comes out as a perfect JSON when I print using text(jsonradius) function. 使用标志,我确保逗号不放在最后一个条目之后,并且当我使用text(jsonradius)函数打印时,逗号作为完美的JSON出现。 '[{"radius":10,"radius":20,"radius":30}]'

But when I try to access it like as follows, no value is returned. 但是当我尝试如下访问它时,没有返回值。

'd3.selectAll("circle").data(jsonradius).enter().select("circle").attr("r",function(d){return d.radius;});'

I am new to d3 and javascript. 我是d3和javascript的新手。 Pls help how to build json dynamically. 请帮助如何动态构建json。

Your JSON object contains 3 fields with the same name. 您的JSON对象包含3个同名字段。 It is an error. 这是一个错误。 It must be something like this: '[{"radius":10},{"radius":20},{"radius":30}]' 它必须是这样的: '[{"radius":10},{"radius":20},{"radius":30}]'

You do not need to convert the data to JSON to be able to access it via d3 . 您无需将数据转换为JSON即可通过d3进行访问。

What you want can be done using the radius array itself: 您可以使用radius数组本身完成所需的操作:

d3.selectAll("circle").data([10, 20, 30]) // Use your array here
  .enter()
  .append('circle')  // not .select('circle')
  .attr('r', function (d) { return d; })
  // other properties.

And, for sake of completeness, you can use JSON.strinfigy to convert strings to and from JSON: 并且,出于完整性考虑,您可以使用JSON.strinfigy在JSON JSON.strinfigy进行字符串转换:

jsonRadius = JSON.stringify(radius.map(function (r) { return { radius: r }; }));

You should not really attempt to build the JSON by concatenation as this could easily result in something unexpected happen. 您不应该真正尝试通过串联来构建JSON,因为这很容易导致意外情况。 Depending what you later do with the string it could be a vector to attach whatever you are building. 根据您以后对字符串的处理方式,它可能是附加您正在构建的内容的向量。 Use the browsers JSON encoder ... 使用浏览器JSON编码器...

radius = [10,20,30];
data = radius.map(function(d){return {radius: d};});
JSON.stringify(data);

This will work in 这将在

  • IE8+ IE8 +
  • Firefox 3.1+ Firefox 3.1+
  • Safari 4.0.3+ Safari 4.0.3+
  • Opera 10.5+ 歌剧10.5+
  • Check other browsers at Can I Use 我可以使用时检查其他浏览器

If you need to support a browser which does not have built in parsing support then consider using a well tested solution to do the encoding so that the correct sanitation is preformed. 如果您需要支持没有内置解析支持的浏览器,请考虑使用经过良好测试的解决方案进行编码,以便正确执行卫生要求。 A good example is json2.js which detects if there is no native solution and 'polyfils' its own solution if needed. 一个很好的例子是json2.js ,它检测是否没有本机解决方案,并在需要时“ polyfils”自己的解决方案。

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

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