使用节点请求模块,我通过URL获取JSON数据。 JSON文件嵌套在多个级别中,而不是以可以重用的方式组织。当前的JSON看起来像这样:
{
"data": {
"series": {
"brochure_image": "range.jpg",
"background_image": "range.jpg",
"Model 1": {
"medium_images": "Model_1.png",
"brand": "Tesla",
"Model 101": {
"medium_images": "Model_101.png",
"brochure_image": "Model_101.png",
},
"Model 102": {
"medium_images": "Model_102.png",
"brochure_image": "Model_102.png",
}
},
"Model 2": {
"medium_images": "Model_2.png",
"brand": "Tesla",
"Model 201": {
"medium_images": "Model_201.png",
"brochure_image": "Model_201.png",
}
}
}
}
}
我想重组,以便我可以轻松地从前端访问数据。我想改变这样:
[
{
"series":"Model 1",
"model": [
{
"modelName": "Model 101",
"medium_images": "Model_101.png",
"brochure_image": "Model_101.png"
},
{
"modelName": "Model 102",
"medium_images": "Model_102.png",
"brochure_image": "Model_102.png"
}
]
}
{
"series":"Model 2",
"model": [
{
"modelName": "Model 201",
"medium_images": "Model_101.png",
"brochure_image": "Model_101.png"
}
]
}
]
我该怎么做?我的节点server.js获取部分如下所示:
const dataUrl = "http://example.com/data/series-data.json";
// API
app.get("/api/getSeriesData", (req, res) => {
request.get(dataUrl, (error, response, body) => {
var json = JSON.parse(body);
var seriesObj = json.data.series;
var seriesArr = [];
for (var series in seriesObj) {
if (seriesObj.hasOwnProperty(series)) {
seriesArr.push({"series": series, "model": seriesObj[series]});
}
}
res.send(seriesArr);
});
});
答案 0 :(得分:2)
您可以这样做:
var obj = {
"data": {
"series": {
"brochure_image": "range.jpg",
"background_image": "range.jpg",
"Model 1": {
"medium_images": "Model_1.png",
"brand": "Tesla",
"Model 101": {
"medium_images": "Model_101.png",
"brochure_image": "Model_101.png",
},
"Model 102": {
"medium_images": "Model_102.png",
"brochure_image": "Model_102.png",
}
},
"Model 2": {
"medium_images": "Model_2.png",
"brand": "Tesla",
"Model 201": {
"medium_images": "Model_201.png",
"brochure_image": "Model_201.png",
}
}
}
}
};
var result = [];
Object.keys(obj.data.series).forEach(key => {
if(key.indexOf("Model") === 0){
var o = {series: key, model:[]};
Object.keys(obj.data.series[key]).forEach(k => {
if(k.indexOf("Model") === 0){
o.model.push({modelName: k, medium_image: obj.data.series[key][k].medium_image, brochure_image: obj.data.series[key][k].brochure_image});
}
});
result.push(o);
}
});
console.log(result);
这样做是为了遍历.data.series
和data.series[MODEL NAME]
的所有属性,并且只考虑属性名称以模型开头的那些值。如果不是您感兴趣的所有值的属性名称都以模型开头,则此方法将不起作用。在这些情况下,您可能必须使用否定断言(检查其属性名称是否是您不感兴趣的已知属性之一)。
答案 1 :(得分:1)
这是我的解决方案。
我们可以删除对象中不需要的keys
,然后遍历对象属性并将它们添加到数组中。
如果您遇到任何问题或疑问,请通过该片段告诉我。
var data = {
"data": {
"series": {
"brochure_image": "range.jpg",
"background_image": "range.jpg",
"Model 1": {
"medium_images": "Model_1.png",
"brand": "Tesla",
"Model 101": {
"medium_images": "Model_101.png",
"brochure_image": "Model_101.png",
},
"Model 102": {
"medium_images": "Model_102.png",
"brochure_image": "Model_102.png",
}
},
"Model 2": {
"medium_images": "Model_2.png",
"brand": "Tesla",
"Model 201": {
"medium_images": "Model_201.png",
"brochure_image": "Model_201.png",
}
}
}
}
};
var output = [];
delete data.data.series.brochure_image
delete data.data.series.background_image
for (k in data.data.series) {
var temp = [];
delete data.data.series[k].medium_images;
delete data.data.series[k].brand;
for (j in data.data.series[k]) {
temp.push({
modelName: j,
medium_images: data.data.series[k][j].medium_images,
brochure_image: data.data.series[k][j].brochure_image
});
}
output.push({
"series": k,
model: temp
});
}
console.log(output);

.as-console {
height: 100%;
}
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}