通过JavaScript/C#动态创建JSON对象
本文关键字:创建 JSON 对象 动态 JavaScript 通过 | 更新日期: 2023-09-27 18:20:56
我有JSON数据
var obj=fields: {
ProductName: { type: "string" },
UnitPrice: { type: "number" },
UnitsInStock: { type: "number" },
Discontinued: { type: "boolean" }
.
.
.
n number of properties
}
我的实际问题是字段可以有n个属性。有人能帮我吗?
提前感谢
您可以循环它:
var obj = {
fields: {
ProductName: {
type: "string"
},
UnitPrice: {
type: "number"
},
UnitsInStock: {
type: "number"
},
Discontinued: {
type: "boolean"
}
}
};
var n = 100;
for (var i = 0; i < n; i++) {
obj.fields["prop" + i] = {type: "string"};
}
console.log(obj);
JSFIDDLE