封装一个Js 对象 生成Json
Posted 丿丶小脚丫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装一个Js 对象 生成Json相关的知识,希望对你有一定的参考价值。
<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的主要内容,如果未能解决你的问题,请参考以下文章
简述什么是ajaxjavascriptjsonJquery?
怎么用jsp生成json格式数据,然后用js又怎样读取其数据?
错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”