js 生成json对象、集合

1. 常规json

var json = {};
json["id"] = 123;
json["value"] = "值";
//转json字符串
alert(JSON.stringify(json));

输出:

{"id":123,"value":"值"}

 

2. 集合json

var jsonArr = [];

var eachJson = {};
eachJson["id"] = 1;
eachJson["value"] = "值1";
jsonArr[0] = eachJson;

eachJson["id"] = 2;
eachJson["value"] = "值2";
jsonArr[1] = eachJson;

eachJson["id"] = 3;
eachJson["value"] = "值3";
jsonArr[2] = eachJson;

alert(JSON.stringify(jsonArr));

输出

[{"id":3,"value":"值3"},{"id":3,"value":"值3"},{"id":3,"value":"值3"}]

你可能感兴趣的:(js,jquery)