javascript 对象转换 json 的插件

下载地址:http://code.google.com/p/jquery-json/

之前写了篇C#后台声明JSON的文章,今天在写个前端声明JSON的,呵呵

http://www.cnblogs.com/linfei721/archive/2013/04/27/3046822.html

多行模式:

var json = new Array();
for (var i = 8; i < 11; i++) {   json.push({ name: "linfei" + i, age: (i + 20), address: "武汉" }); } $(":text").val($.toJSON(json));

显示结果:

[
    {
        "name": "linfei8",
        "age": 28,
        "address": "武汉"
    },
    {
        "name": "linfei9",
        "age": 29,
        "address": "武汉"
    },
    {
        "name": "linfei10",
        "age": 30,
        "address": "武汉"
    }
]

多行多对象模式

var json = new Array();
for (var i = 8; i < 11; i++) {
    json.push({ name: "linfei" + i, age: (i + 20), address: "武汉" });
}

var list = {
header: { name: "名称", age: "年龄", address: "地址" },rows: json } 

$(
":text").val($.toJSON(list));

显示结果:

{
    "header": {
        "name": "名称",
        "age": "年龄",
        "address": "地址"
    },
    "rows": [
        {
            "name": "linfei8",
            "age": 28,
            "address": "武汉"
        },
        {
            "name": "linfei9",
            "age": 29,
            "address": "武汉"
        },
        {
            "name": "linfei10",
            "age": 30,
            "address": "武汉"
        }
    ]
}

 

posted @ 2013-05-07 16:53  林飞  阅读(606)  评论(0编辑  收藏  举报