码迷,mamicode.com
首页 > Web开发 > 详细

封装一个Js 对象 生成Json

时间:2017-12-06 14:39:49      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:www   min   script   ++   对象   string   src   ring   scripts   

<script src="~/Content/Scripts/jquery-1.11.3.min.js"></script>
<script>
    $(function () {

        var data =
            {
                GoThroughNo: "ABC00001",
                AwbList: []
            };


        for (var i = 0; i < 10; i++) {
            var hawbMapping = new Object();
            hawbMapping.Mawb = i;
            hawbMapping.Hawb = i + "001";
            hawbMapping.PictrueUrls = ["http://www.baidu.com/", "http://sina.com.cn"];
            data.AwbList.push(hawbMapping);
        }


        var jsonString = JSON.stringify(data);

        alert(jsonString);
    });
</script>

 

生成的Json如下:

{
    "GoThroughNo": "ABC00001",
    "AwbList": [
        {
            "Mawb": 0,
            "Hawb": "0001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 1,
            "Hawb": "1001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 2,
            "Hawb": "2001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 3,
            "Hawb": "3001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 4,
            "Hawb": "4001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 5,
            "Hawb": "5001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 6,
            "Hawb": "6001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 7,
            "Hawb": "7001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 8,
            "Hawb": "8001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        },
        {
            "Mawb": 9,
            "Hawb": "9001",
            "PictrueUrls": [
                "http://www.baidu.com/",
                "http://sina.com.cn"
            ]
        }
    ]
}

 

封装一个Js 对象 生成Json

标签:www   min   script   ++   对象   string   src   ring   scripts   

原文地址:http://www.cnblogs.com/TimeMaster/p/7992238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!