首页 > 代码库 > 如何用js定义数组,用js来拼接json字段

如何用js定义数组,用js来拼接json字段

定义js数组的方式有:

var arr = ();var arr = [];var arr = new Array();

如何拼接成一个json字段。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD>  <TITLE> obj TO JSON Demo </TITLE>  <META NAME="Generator" CONTENT="EditPlus">  <META NAME="Author" CONTENT="">  <META NAME="Keywords" CONTENT="">  <META NAME="Description" CONTENT=""> </HEAD><SCRIPT type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></SCRIPT><script type="text/javascript" src="http://www.mamicode.com/js/jquery.json-2.4.min.js"></script> <BODY> <h2>利用jquery-json将js对象转化为JSON床</h2> <INPUT TYPE="button" VALUE="http://www.mamicode.com/生成JSON串" ONCLICK="" id="testBut"><br/> <div id="showDiv" style="display:none">     <div> </BODY> <script type="text/javascript">    function student(sid,name,age,classInfo){        this.sid=sid;        this.name=name;        this.aeg=age;        this.classInfo=classInfo;    }    function classInfo(classname,teacher){        this.classname=classname;        this.teacher=teacher;    }    $(function(){        $("#testBut").click(function(){            var students = new Array();                arry = new classInfo(‘三年级一班‘,‘项正伟‘);                students[0]=new student(2014001,‘蔡子旭‘,23,arry);                students[1]=new student(2014002,‘张苞‘,24,arry);                students[2]=new student(2014003,‘张颌‘,33,arry);            var jsonStr=$.toJSON(students);            $("#showDiv").show();            $("#showDiv").text(jsonStr);        });    }); </script></HTML>

 

如何用js定义数组,用js来拼接json字段