如何创建没有键的json数组

时间:2016-06-27 04:24:25

标签: d3.js

{"":[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},
{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},
{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},
{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},
{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]}

我有一个像这样的json数组,我需要输入d3js bt我希望它像这样

[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},
{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},
{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},
{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},
{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}] 

我怎么得到它?

2 个答案:

答案 0 :(得分:1)

想象一下你的json数据是这样的:

var k = {"":[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]}

所以你需要这样做:k[""]为你提供所需的JSON

[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]

答案 1 :(得分:0)

使用数组索引。 for (var i=0; i < 5; i++) { array [i] = {...} }其中'array'是变量的名称。顺便说一句,为什么不把年份作为关键?