I have this JSON data:
我有这个JSON数据:
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
}
Suppose I don't know how many columns and rows of employees I have, how do I create this object in JavaScript (Without concate strings)? Assume that I get each row in "onGeneratedRow" method, and I need to push each column (firstName, lastName) to the '{}' brackets.
假设我不知道我有多少列和行雇员,我如何用JavaScript(不考虑字符串)创建这个对象?假设我在“onGeneratedRow”方法中获得了每一行,我需要将每一列(firstName, lastName)推到“{}”括号中。
var viewData = {
employees : []
};
var rowNum = -1;
function onGeneratedRow(columnsResult)
{
rowNum = rowNum + 1;
viewData.employees.push({});
columnsResult.forEach(function(column) {
var columnName = column.metadata.colName;
viewData.employees[rowNum][columnName] = column.value; });
}
3 个解决方案
#1
113
This is what you need!
这就是你需要的!
function onGeneratedRow(columnsResult)
{
var jsonData = {};
columnsResult.forEach(function(column)
{
var columnName = column.metadata.colName;
jsonData[columnName] = column.value;
});
viewData.employees.push(jsonData);
}
#2
60
Perhaps this information will help you.
也许这些信息会对你有所帮助。
var sitePersonel = {};
var employees = []
sitePersonel.employees = employees;
console.log(sitePersonel);
var firstName = "John";
var lastName = "Smith";
var employee = {
"firstName": firstName,
"lastName": lastName
}
sitePersonel.employees.push(employee);
console.log(sitePersonel);
var manager = "Jane Doe";
sitePersonel.employees[0].manager = manager;
console.log(sitePersonel);
console.log(JSON.stringify(sitePersonel));
#3
5
This topic, especially the answer of Xotic750 was very helpful to me. I wanted to generate a json variable to pass it to a php script using ajax. My values were stored into two arrays, and i wanted them in json format. This is a generic example:
这个话题,尤其是Xotic750的答案对我很有帮助。我想生成一个json变量,并使用ajax将其传递给php脚本。我的值存储在两个数组中,我希望它们是json格式。这是一个一般的例子:
valArray1 = [121, 324, 42, 31];
valArray2 = [232, 131, 443];
myJson = {objArray1: {}, objArray2: {}};
for (var k = 1; k < valArray1.length; k++) {
var objName = 'obj' + k;
var objValue = valArray1[k];
myJson.objArray1[objName] = objValue;
}
for (var k = 1; k < valArray2.length; k++) {
var objName = 'obj' + k;
var objValue = valArray2[k];
myJson.objArray2[objName] = objValue;
}
console.log(JSON.stringify(myJson));
The result in the console Log should be something like this:
控制台日志中的结果应该是这样的:
{
"objArray1": {
"obj1": 121,
"obj2": 324,
"obj3": 42,
"obj4": 31
},
"objArray2": {
"obj1": 232,
"obj2": 131,
"obj3": 443
}
}
#1
113
This is what you need!
这就是你需要的!
function onGeneratedRow(columnsResult)
{
var jsonData = {};
columnsResult.forEach(function(column)
{
var columnName = column.metadata.colName;
jsonData[columnName] = column.value;
});
viewData.employees.push(jsonData);
}
#2
60
Perhaps this information will help you.
也许这些信息会对你有所帮助。
var sitePersonel = {};
var employees = []
sitePersonel.employees = employees;
console.log(sitePersonel);
var firstName = "John";
var lastName = "Smith";
var employee = {
"firstName": firstName,
"lastName": lastName
}
sitePersonel.employees.push(employee);
console.log(sitePersonel);
var manager = "Jane Doe";
sitePersonel.employees[0].manager = manager;
console.log(sitePersonel);
console.log(JSON.stringify(sitePersonel));
#3
5
This topic, especially the answer of Xotic750 was very helpful to me. I wanted to generate a json variable to pass it to a php script using ajax. My values were stored into two arrays, and i wanted them in json format. This is a generic example:
这个话题,尤其是Xotic750的答案对我很有帮助。我想生成一个json变量,并使用ajax将其传递给php脚本。我的值存储在两个数组中,我希望它们是json格式。这是一个一般的例子:
valArray1 = [121, 324, 42, 31];
valArray2 = [232, 131, 443];
myJson = {objArray1: {}, objArray2: {}};
for (var k = 1; k < valArray1.length; k++) {
var objName = 'obj' + k;
var objValue = valArray1[k];
myJson.objArray1[objName] = objValue;
}
for (var k = 1; k < valArray2.length; k++) {
var objName = 'obj' + k;
var objValue = valArray2[k];
myJson.objArray2[objName] = objValue;
}
console.log(JSON.stringify(myJson));
The result in the console Log should be something like this:
控制台日志中的结果应该是这样的:
{
"objArray1": {
"obj1": 121,
"obj2": 324,
"obj3": 42,
"obj4": 31
},
"objArray2": {
"obj1": 232,
"obj2": 131,
"obj3": 443
}
}