js怎样生成json的数据

var row1 = {};
row1.name = 'david';
row1.age = '20';

//或者
var row2 = {name: 'peter', age: '23'};

var data = [];
data.push(row1);
data.push(row2);

//此时data的json字符串为 [{name:'david',age:'20'},{name:'peter',age'23'}]
//如果后台是PHP,将得到
/*
print_r($_GET);
Array(0=>array('name' => 'david'....
*/

但是建议采用如下的形式
var data = {records: []};
data.records.push(row1);
data.records.push(row2);
//此时data的json字符串为 {records:[{name:'david',age:'20'},{name:'peter',age'23'}]}

//如果后台是PHP,将得到
/*
print_r($_GET);
Array(
'records' => array(

0 => array(

'name' => 'david'

....

*/

2, 也可以下面方式实现:
var data = [];
data.push({name: 'peter', age: '23'});
posted @ 2014-05-14 16:48  jimcsharp  阅读(688)  评论(0编辑  收藏  举报