我正在尝试将CSV文件转换为JSON嵌套对象。我已经完成了任务,但是我已经对字段进行了硬编码,如果CSV字段发生了变化,我也必须修改代码。因此,我正在寻找一种动态的方法。
假设我已经从CSV文件中读取了行,并创建了以下数组:
var csv = ["ChapterIndex;ArticleIndex;URL;language/chapter/en;language/title/en;language/bodyText/en;language/keywords/en;language/languageCode/en;language/chapter/es;language/title/es;language/bodyText/es;language/keywords/es;language/languageCode/es",
"1;1;www.hotmail.com;Overview-en;Article1-en;BodyText1-en;key1,key2;en;Overview-es;Article1-es;BodyText1-es;key1,key2;es",
"1;1;www.google.com;Overview-en;Article2-en;BodyText2-en;key1,key2;en;Overview-es;Article2-es;BodyText2-ens;key1,key2;es"]
我想将以下格式提供给JSON
var obj ={
chaterIndex:1,
articleIndex:1,
url:"www.test.com"
language:[
{ chapter:"Overvie-en",
title:"Article1-en",
bodyText:"bodyText-en",
keywords:"key1,key2",
languageCode:"en"
},
{ chapter:"Overvie-es",
title:"Article1-es",
bodyText:"bodyText-es",
keywords:"key1,key2",
languageCode:"es"
}]
}
到目前为止,我已经完成了一半任务,但是我的逻辑没有进一步发展,我想请您提供帮助。
var csv = ["ChapterIndex;ArticleIndex;URL;language/chapter/en;language/title/en;language/bodyText/en;language/keywords/en;language/languageCode/en;language/chapter/es;language/title/es;language/bodyText/es;language/keywords/es;language/languageCode/es",
"1;1;www.hotmail.com;Overview-en;Article1-en;BodyText1-en;key1,key2;en;Overview-es;Article1-es;BodyText1-es;key1,key2;es",
"1;1;www.google.com;Overview-en;Article2-en;BodyText2-en;key1,key2;en;Overview-es;Article2-es;BodyText2-ens;key1,key2;es"]
var attrs = csv.splice(0,1);
var articles = csv
var result = csv.map(function(row) {
var obj = {};
var rowData = row.split(';');
attrs[0].split(';').forEach(function(val, idx) {
obj = constructObj(val, obj, rowData[idx]);
});
return obj;
})
function constructObj(str, parentObj, data) {
if(str.split('/').length === 1) {
parentObj[str] = data;
return parentObj;
}
var languages = [];
var curKey = str.split('/')[0];
if(!parentObj[curKey])
parentObj[curKey] = {};
parentObj[curKey] = constructObj(str.split('/').slice(1).join('/'), parentObj[curKey], data);
return parentObj;
}
console.log(result);
最佳答案
var csv = ["ChapterIndex;ArticleIndex;URL;language/chapter/en;language/title/en;language/bodyText/en;language/keywords/en;language/languageCode/en;language/chapter/es;language/title/es;language/bodyText/es;language/keywords/es;language/languageCode/es",
"1;1;www.hotmail.com;Overview-en;Article1-en;BodyText1-en;key1,key2;en;Overview-es;Article1-es;BodyText1-es;key1,key2;es",
"1;1;www.google.com;Overview-en;Article2-en;BodyText2-en;key1,key2;en;Overview-es;Article2-es;BodyText2-ens;key1,key2;es"]
function camelCase(str) {
return str
.replace(/\s(.)/g, function(a) {
return a.toUpperCase();
})
.replace(/\s/g, '')
.replace(/^.{0,3}/, function(b) {
return b.toLowerCase();
});
}
var attrs = csv.splice(0,1);
var articles = csv
var result = csv.map(function(row) {
var obj = {};
var rowData = row.split(';');
attrs[0].split(';').forEach(function(val, idx) {
obj = constructObj(val, obj, rowData[idx]);
});
return obj;
})
function constructObj(str, parentObj, data) {
if(str.split('/').length === 1) {
parentObj[camelCase(str)] = data;
return parentObj;
}
// language
var curKey = str.split('/')[0];
if(!parentObj[curKey]) {
parentObj[curKey] = [];
newLang = {}
newLang[str.split('/')[1]] = data;
parentObj[curKey].push(newLang);
} else {
var found = false;
for (var i = 0; i < parentObj[curKey].length; i++) {
if(Object.values(parentObj[curKey][i])[0] !== undefined && Object.values(parentObj[curKey][i])[0].includes(str.split('/').slice(2))) {
parentObj[curKey][i][str.split('/')[1]] = data;
found = true;
break;
}
}
if (!found) {
newLang = {}
newLang[str.split('/')[1]] = data;
parentObj[curKey].push(newLang);
}
}
return parentObj;
}
console.log(result);