通过JavaScript / C动态创建JSON对象#

时间:2014-09-08 06:02:49

标签: c# javascript

我有JSON数据

var obj=fields: {
ProductName: { type: "string" },
UnitPrice: { type: "number" },
UnitsInStock: { type: "number" },
Discontinued: { type: "boolean" }
       .
       .
       .
     n number of properties

}

我的实际问题是,字段可以有n个属性。任何人都可以帮助我。

提前致谢

1 个答案:

答案 0 :(得分:0)

你可以循环它:

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