嵌套的JSON对象数据与dimple.js图表

时间:2016-06-27 15:05:40

标签: javascript json d3.js nested dimple.js

我需要帮助来压缩嵌套格式的json数据。我尝试了一些我在S / O中找到但却没有多大帮助的东西。我花了很多时间用静态数据创建我的图表,我不知道我会以这种格式获取我的数据。任何帮助都感激不尽!我正在使用dimple vertical stack var

 {
  "head": {
    "vars": [ "Country" , "City" , "County" , "Fiscal_Year" , "Fiscal_Qtr" , "Start_Period" , "End_Period" , "Extract_Date" ,  "AA" , "BB" , "CC" , "DD" , "EE" , "FF" , "GG" , "HH" , "II" , "JJ" , "KK" , "LL" , "MM" , "NN" ]
  } ,
  "results": {
    "bindings": [
      {
        "Country": {  "type": "typed-literal" , "value": "United States" } ,
        "City": {  "type": "typed-literal" , "value": "New York" } ,
        "County": {  "type": "typed-literal" , "value": "Manhattan" } ,
        "Fiscal_Year": {  "type": "typed-literal" , "value": "2014" } ,
        "Fiscal_Qtr": { "type": "typed-literal" , "value": "1" } ,
        "Start_Period": { "type": "typed-literal" , "value": "2014-10-01" } ,
        "End_Period": {  "type": "typed-literal" , "value": "2014-12-31" } ,
        "Extract_Date": {"type": "typed-literal" , "value": "2015-01-01" } ,
        "AA": {  "type": "typed-literal" , "value": "0.549" } ,
        "BB": { "type": "typed-literal" , "value": "0.1526" } ,
        "CC": {  "type": "typed-literal" , "value": "0.0258" } ,
        "DD": { "type": "typed-literal" , "value": "0.047400000000000005" } ,
        "EE": { "type": "typed-literal" , "value": "0.21780000000000002" } ,
        "FF": {"type": "typed-literal" , "value": "0.0074" } ,
        "GG": { "type": "typed-literal" , "value": "0.615" } ,
        "HH": { "type": "typed-literal" , "value": "0.507" } ,
        "II": { "type": "literal" , "value": "--" } ,
        "JJ": {"type": "typed-literal" , "value": "0.113" } ,
        "KK": {  "type": "typed-literal" , "value": "0.026" } ,
        "LL": { "type": "typed-literal" , "value": "0.034" } ,
        "MM": { "type": "typed-literal" , "value": "0.318" } ,
        "NN": { "type": "typed-literal" , "value": "0.002" }
      } 

我正在寻找类似这样的输出:

    [
{"Country": "United States"},
{"City": "New York"},
{"County": "Manhattan"},
{"Fiscal_Year": "2014"},
{"Start_Period": "2014-10-01"},
{"End_Period": "2014-12-32"},
{"Extract_Date": "2015-01-01" },
{"AA": "0.549" },
{"BB": "0.1526" },
{"CC": "0.0258" },
{"DD": "0.047400000000000005" },
{"EE":  "0.21780000000000002" },
{"FF": "0.0074" },
{"GG":  "0.615" },
{"HH":  "0.507" },
{"II":  "--" },
{"JJ":  "0.113" },
{"KK":  "0.026" },
{"LL":  "0.034" },
{"MM":  "0.318" },
{"NN":  "0.002" }
]

1 个答案:

答案 0 :(得分:1)

Javascript的Object.keys()接受一个对象(例如{foo: 1, bar: 2})并返回该对象的键["foo", "bar"]的数组。

使用它,您可以像这样转换对象:

data.results.bindings.map(function(original) {
  var converted = {};
  Object.keys(original)
    .forEach(function(key) {
      converted[key] = original[key].value;
    });
  return converted;
})

或者,使用reduce可以实现同样的目的。

data.results.bindings.map(function(original) {
  return Object.keys(original)
    .reduce(function(converted, key) {
      converted[key] = original[key].value;
      return converted;
    }, {});
})

除了第二个选项不依赖于定义var converted,更多地使用“inline-ish”,这使得它更具可读性(至少在一些圈子。)