怎样将javascript转化为json数据
2个回答
展开全部
我举个例子给你,看看能不能对你有所帮助,若有帮助请采纳!
页面有个动态表格,在保存动态表格时,我们需要将其数据获取到,并在js中将其转换为json数据然后在送达给后台Action进行遍历后保存在相应的数据表中。
而你所问的问题,应该是如何在js中 将数据转换为json,请看下面代码:
var submitArrBefore = [];
//根据表格ID获取表格的行数
var beforeTable = document.getElementById("tabEvectionbefore");
//减去表头
var beforelen = (beforeTable.rows.length)-2;
if (beforelen > 0) {
//遍历行数据
for(var i=1;i<=beforelen;i++){
var beforeevectiontime= $("#beforeevectiontime_"+i).val();
var beforepeersite= $("#beforepeersite_"+i).val();
var beforevehicle = $("#beforevehicle_"+i).val();
var beforeplatenumbers =$("#beforeplatenumbers_"+i).val();
var beforestaymoney =$("#beforestaymoney_"+i).val();
var jsonTmp = {};
jsonTmp["evectiontime"] = beforeevectiontime;
jsonTmp["peersite"] = beforepeersite;
jsonTmp["vehicle"] = beforevehicle;
jsonTmp["platenumbers"] = beforeplatenumbers;
jsonTmp["staymoney"] = beforestaymoney;
submitArrBefore.push(jsonTmp);//将数据push到数组中
}
}
//将数组转换为json数据
var evectionPeerBefore = JSON.stringify(submitArrBefore);
//为了方便传递到后台,于是声明了object容器来装。
var object = new Object();
object.evectionPeerBefore = evectionPeerBefore;
展开全部
使用$.toJSON(Object)就可以转换了,转换之前先引入jquery.json.js
/*
* jQuery JSON Plugin
* version: 2.1 (2009-08-14)
*
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*
* Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
* website's http://www.json.org/json2.js, which proclaims:
* "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
* I uphold.
*
* It is also influenced heavily by MochiKit's serializeJSON, which is
* copyrighted 2005 by Bob Ippolito.
*/
(function($) {
/** jQuery.toJSON( json-serializble )
Converts the given argument into a JSON respresentation.
If an object has a "toJSON" function, that will be used to get the representation.
Non-integer/string keys are skipped in the object, as are keys that point to a function.
json-serializble:
The *thing* to be converted.
**/
$.toJSON = function(o)
{
if (typeof(JSON) == 'object' && JSON.stringify)
return JSON.stringify(o);
var type = typeof(o);
if (o === null)
return "null";
if (type == "undefined")
return undefined;
if (type == "number" || type == "boolean")
return o + "";
if (type == "string")
return $.quoteString(o);
/*
* jQuery JSON Plugin
* version: 2.1 (2009-08-14)
*
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*
* Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
* website's http://www.json.org/json2.js, which proclaims:
* "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
* I uphold.
*
* It is also influenced heavily by MochiKit's serializeJSON, which is
* copyrighted 2005 by Bob Ippolito.
*/
(function($) {
/** jQuery.toJSON( json-serializble )
Converts the given argument into a JSON respresentation.
If an object has a "toJSON" function, that will be used to get the representation.
Non-integer/string keys are skipped in the object, as are keys that point to a function.
json-serializble:
The *thing* to be converted.
**/
$.toJSON = function(o)
{
if (typeof(JSON) == 'object' && JSON.stringify)
return JSON.stringify(o);
var type = typeof(o);
if (o === null)
return "null";
if (type == "undefined")
return undefined;
if (type == "number" || type == "boolean")
return o + "";
if (type == "string")
return $.quoteString(o);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询