将JSON数据转换为表格数据

时间:2015-11-12 22:51:05

标签: javascript json tabular

我有一些JSON数据如下,我想使用javascript将其转换为表格格式。谁能请我朝正确的方向推?

{
"definition": {
    "accountID": 111,
    "profileID": "12345",
    "ID": "6789",
    "name": "Content Groups and Sub-Groups",
    "description": "Some desc",
    "language": null,
    "timezone": "UTC 1",
    "dimensions": [{
        "ID": "contentgroup",
        "name": "Content Group"
    }, {
        "ID": "xIfaV44n7l5",
        "name": "Content Sub-Group"
    }],
    "measures": [{
        "name": "Visits",
        "ID": "Visits",
        "columnID": 0,
        "measureFormatType": null
    }, {
        "name": "ofVisits",
        "ID": "PercentOfVisits",
        "columnID": 1,
        "measureFormatType": null
    }, {
        "name": "PageViews",
        "ID": "CAoNYspmFb5",
        "columnID": 2,
        "measureFormatType": null
    }, {
        "name": "AverageTimeViewedseconds",
        "ID": "fC51LCSmFb5",
        "columnID": 3,
        "measureFormatType": null
    }]
},
"data": [{
    "period": "Day",
    "start_date": "2015-10-16",
    "end_date": "2015-11-12",
    "attributes": {},
    "measures": {
        "Visits": null,
        "PageViews": null,
        "ofVisits": null,
        "AverageTimeViewedseconds": 49.2952872487968
    },
    "SubRows": [{
        "Item 1": {
            "attributes": {},
            "measures": {
                "Visits": 422093,
                "PageViews": 3696881,
                "ofVisits": 59.5037745557584,
                "AverageTimeViewedseconds": 45.570268226002
            },
            "SubRows": {
                "None": {
                    "attributes": {},
                    "measures": {
                        "Visits": 393767,
                        "PageViews": 2817399,
                        "ofVisits": 93.28915665505,
                        "AverageTimeViewedseconds": 47.9638063415244
                    },
                    "SubRows": null
                },
                "Pv": {
                    "attributes": {},
                    "measures": {
                        "Visits": 179418,
                        "PageViews": 500232,
                        "ofVisits": 42.5067461436224,
                        "AverageTimeViewedseconds": 39.5274551506911
                    },
                    "SubRows": null
                },
                "us": {
                    "attributes": {},
                    "measures": {
                        "Visits": 120908,
                        "PageViews": 294125,
                        "ofVisits": 28.6448721016458,
                        "AverageTimeViewedseconds": 36.9675992197275
                    },
                    "SubRows": null
                },
                "dm": {
                    "attributes": {},
                    "measures": {
                        "Visits": 26214,
                        "PageViews": 49640,
                        "ofVisits": 6.21047968101816,
                        "AverageTimeViewedseconds": 33.6100170140366
                    },
                    "SubRows": null
                },
                "bn": {
                    "attributes": {},
                    "measures": {
                        "Visits": 18915,
                        "PageViews": 35485,
                        "ofVisits": 4.48123991632176,
                        "AverageTimeViewedseconds": 35.896700789529
                    },
                    "SubRows": null
                }
            }
        },
        "CR": {
            "attributes": {},
            "measures": {
                "Visits": 6357,
                "PageViews": 14999,
                "ofVisits": 0.896166235523821,
                "AverageTimeViewedseconds": 57.5707590422822
            },
            "SubRows": {
                "None": {
                    "attributes": {},
                    "measures": {
                        "Visits": 6357,
                        "PageViews": 14999,
                        "ofVisits": 100,
                        "AverageTimeViewedseconds": 57.5707590422822
                    },
                    "SubRows": null
                }
            }
        },
        "HP": {
            "attributes": {},
            "measures": {
                "Visits": 204003,
                "PageViews": 347290,
                "ofVisits": 28.7589429834145,
                "AverageTimeViewedseconds": 71.5667093018127
            },
            "SubRows": {
                "None": {
                    "attributes": {},
                    "measures": {
                        "Visits": 204003,
                        "PageViews": 347290,
                        "ofVisits": 100,
                        "AverageTimeViewedseconds": 71.5667093018127
                    },
                    "SubRows": null
                }
            }
        },
        "NC": {
            "attributes": {},
            "measures": {
                "Visits": 12027,
                "PageViews": 39166,
                "ofVisits": 1.69548392553799,
                "AverageTimeViewedseconds": 68.6549936328907
            },
            "SubRows": {
                "Details": {
                    "attributes": {},
                    "measures": {
                        "Visits": 9735,
                        "PageViews": 24652,
                        "ofVisits": 80.9428785233225,
                        "AverageTimeViewedseconds": 91.8467462260302
                    },
                    "SubRows": null
                },
                "None": {
                    "attributes": {},
                    "measures": {
                        "Visits": 4559,
                        "PageViews": 14514,
                        "ofVisits": 37.9063773177018,
                        "AverageTimeViewedseconds": 34.6529086286825
                    },
                    "SubRows": null
                }
            }
        },
        "News": {
            "attributes": {},
            "measures": {
                "Visits": 1,
                "PageViews": 1,
                "ofVisits": 0.000140973137568636,
                "AverageTimeViewedseconds": 31
            },
            "SubRows": {
                "Results": {
                    "attributes": {},
                    "measures": {
                        "Visits": 1,
                        "PageViews": 1,
                        "ofVisits": 100,
                        "AverageTimeViewedseconds": 31
                    },
                    "SubRows": null
                }
            }
        },
        "N&R": {
            "attributes": {},
            "measures": {
                "Visits": 56464,
                "PageViews": 147583,
                "ofVisits": 7.95990723967548,
                "AverageTimeViewedseconds": 96.5128803993922
            },
            "SubRows": {
                "Nws": {
                    "attributes": {},
                    "measures": {
                        "Visits": 32330,
                        "PageViews": 53279,
                        "ofVisits": 57.2577217342023,
                        "AverageTimeViewedseconds": 111.729443098112
                    },
                    "SubRows": null
                },
                "Res": {
                    "attributes": {},
                    "measures": {
                        "Visits": 21931,
                        "PageViews": 55061,
                        "ofVisits": 38.8406772456787,
                        "AverageTimeViewedseconds": 49.3369502250345
                    },
                    "SubRows": null
                },
                "Rev": {
                    "attributes": {},
                    "measures": {
                        "Visits": 18589,
                        "PageViews": 25744,
                        "ofVisits": 32.9218617172003,
                        "AverageTimeViewedseconds": 166.81998318091
                    },
                    "SubRows": null
                },
                "Video": {
                    "attributes": {},
                    "measures": {
                        "Visits": 4238,
                        "PageViews": 5710,
                        "ofVisits": 7.50566732785492,
                        "AverageTimeViewedseconds": 148.672823218997
                    },
                    "SubRows": null
                },
                "None": {
                    "attributes": {},
                    "measures": {
                        "Visits": 3957,
                        "PageViews": 5432,
                        "ofVisits": 7.00800510059507,
                        "AverageTimeViewedseconds": 50.8313197026022
                    },
                    "SubRows": null
                },
                "Adv": {
                    "attributes": {},
                    "measures": {
                        "Visits": 1923,
                        "PageViews": 2207,
                        "ofVisits": 3.40570983281383,
                        "AverageTimeViewedseconds": 200.537755822159
                    },
                    "SubRows": null
                },
                "Vids": {
                    "attributes": {},
                    "measures": {
                        "Visits": 65,
                        "PageViews": 80,
                        "ofVisits": 0.11511759705299,
                        "AverageTimeViewedseconds": 56.5490196078431
                    },
                    "SubRows": null
                },
                "Feat": {
                    "attributes": {},
                    "measures": {
                        "Visits": 35,
                        "PageViews": 53,
                        "ofVisits": 0.0619863984131482,
                        "AverageTimeViewedseconds": 52.0526315789474
                    },
                    "SubRows": null
                },
                "Ind": {
                    "attributes": {},
                    "measures": {
                        "Visits": 11,
                        "PageViews": 15,
                        "ofVisits": 0.0194814395012752,
                        "AverageTimeViewedseconds": 24.75
                    },
                    "SubRows": null
                },
                "Fin": {
                    "attributes": {},
                    "measures": {
                        "Visits": 1,
                        "PageViews": 2,
                        "ofVisits": 0.00177103995466138,
                        "AverageTimeViewedseconds": 584
                    },
                    "SubRows": null
                }
            }
        },
        "PandA": {
            "attributes": {},
            "measures": {
                "Visits": 154,
                "PageViews": 505,
                "ofVisits": 0.02170986318557,
                "AverageTimeViewedseconds": 49.5526992287918
            },
            "SubRows": {
                "None": {
                    "attributes": {},
                    "measures": {
                        "Visits": 154,
                        "PageViews": 505,
                        "ofVisits": 100,
                        "AverageTimeViewedseconds": 49.5526992287918
                    },
                    "SubRows": null
                }
            }
        },
        "Val": {
            "attributes": {},
            "measures": {
                "Visits": 8256,
                "PageViews": 38944,
                "ofVisits": 1.16387422376666,
                "AverageTimeViewedseconds": 44.9641971481711
            },
            "SubRows": {
                "None": {
                    "attributes": {},
                    "measures": {
                        "Visits": 8256,
                        "PageViews": 38944,
                        "ofVisits": 100,
                        "AverageTimeViewedseconds": 44.9641971481711
                    },
                    "SubRows": null
                }
            }
        }
    }]
}]

}

我想要的输出如下。请注意,SubRows可以达到N级深度



<table>
  <tr>
    <td>Content Group</td>
    <td>Content Sub-Group</td>
    <td>Visits</td>
    <td>ofVisits</td>
    <td>PageViews</td>
    <td>AverageTimeViewedseconds</td>
  </tr>
  <tr>
    <td>Item 1</td>
    <td>None</td>
    <td>163086</td>
    <td>93.10740527</td>
    <td>1164146</td>
    <td>48.83809909</td>
  </tr>
  <tr>
    <td>Item 1</td>
    <td>Pv</td>
    <td>74305</td>
    <td>42.42145708</td>
    <td>205886</td>
    <td>39.95980053</td>
  </tr>
  <tr>
    <td>Item 1</td>
    <td>us</td>
    <td>51102</td>
    <td>29.17463562</td>
    <td>122519</td>
    <td>37.33043606</td>
  </tr>
  <tr>
    <td>Item 1</td>
    <td>dm</td>
    <td>11359</td>
    <td>6.484965089</td>
    <td>21204</td>
    <td>33.79432872</td>
  </tr>
  <tr>
    <td>Item 1</td>
    <td>bn</td>
    <td>7825</td>
    <td>4.467369647</td>
    <td>14386</td>
    <td>36.8456341</td>
  </tr>
  <tr>
    <td>CR</td>
    <td>None</td>
    <td>2815</td>
    <td>100</td>
    <td>6867</td>
    <td>49.56078259</td>
  </tr>
  <tr>
    <td>HP</td>
    <td>None</td>
    <td>88100</td>
    <td>100</td>
    <td>149623</td>
    <td>74.57752354</td>
  </tr>
  <tr>
    <td>NC</td>
    <td>Details</td>
    <td>4023</td>
    <td>80.73449729</td>
    <td>9843</td>
    <td>96.52630225</td>
  </tr>
  <tr>
    <td>NC</td>
    <td>None</td>
    <td>1873</td>
    <td>37.58779851</td>
    <td>5781</td>
    <td>34.08170181</td>
  </tr>
  <tr>
    <td>News</td>
    <td>Results</td>
    <td>13779</td>
    <td>56.92155162</td>
    <td>22701</td>
    <td>113.0970086</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Nws</td>
    <td>9520</td>
    <td>39.32746726</td>
    <td>23642</td>
    <td>49.63287371</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Res</td>
    <td>7701</td>
    <td>31.81311191</td>
    <td>10841</td>
    <td>158.9755531</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Rev</td>
    <td>2164</td>
    <td>8.939562936</td>
    <td>2767</td>
    <td>147.957504</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Video</td>
    <td>1740</td>
    <td>7.18800347</td>
    <td>2393</td>
    <td>55.19688004</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>None</td>
    <td>770</td>
    <td>3.180898087</td>
    <td>896</td>
    <td>211.3592401</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Adv</td>
    <td>13</td>
    <td>0.053703474</td>
    <td>24</td>
    <td>52.27272727</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Vids</td>
    <td>6</td>
    <td>0.024786219</td>
    <td>6</td>
    <td>145</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Feat</td>
    <td>4</td>
    <td>0.016524146</td>
    <td>5</td>
    <td>18</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Ind</td>
    <td>66</td>
    <td>100</td>
    <td>243</td>
    <td>48.09424084</td>
  </tr>
  <tr>
    <td>N&R</td>
    <td>Fin</td>
    <td>3522</td>
    <td>100</td>
    <td>16518</td>
    <td>44.23769833</td>
  </tr>
  <tr>
    <td></td>
  </tr>
</table>
&#13;
&#13;
&#13;

我的调解解决方案

var measureNames = [];                         var dimensionNames = [];

                    Object.keys(data.definition.dimensions).forEach(function(dimension, idx) {
                        dimensionNames.push(data.definition.dimensions[dimension].name);
                    });
                    Object.keys(data.definition.measures).forEach(function(measure, idx) {
                        measureNames.push(data.definition.measures[measure].name);
                    });

    var jsonDataArray = [];
walk(data.data[0].SubRows[0], [], measureNames, jsonDataArray);

var walk = function (subrows, cells, fieldNames, jsonDataArray) {
                    Object.keys(subrows).forEach(function(key, idx) {
                        var row = subrows[key];
                        var columns = {};
                        cells.push(key);
                        columns['dimensionValues'] = cells.slice(0);

                        if (row.SubRows == null) {
                            Object.keys(row.measures).forEach(function(measure, idx) {
                                columns['measureValues'] = row.measures;
                            });

                            var jsonData = {};
                            jsonData['dimensionValues'] = columns.dimensionValues;

                            for (var i = 0; i < fieldNames.length; i++) {
                                var columnName = fieldNames[i];
                                jsonData[columnName] = columns.measureValues[columnName];
                            }
                            jsonDataArray.push(jsonData);
                            cells = [];

                        } else {
                            walk(row.SubRows, columns.dimensionValues, fieldNames, jsonDataArray);
                        }

                    });

由于

0 个答案:

没有答案