掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
如果你現(xiàn)在正在使用Restful API,并且你需要通過(guò)web項(xiàng)目來(lái)構(gòu)建JSON格式字符串的響應(yīng),那么這篇文章將幫助你使用javascript來(lái)創(chuàng)建json格式字符串。這是非常有用的,我們將通過(guò)jQuery插件$.toJSON把數(shù)據(jù)對(duì)象轉(zhuǎn)換為json格式。

成都創(chuàng)新互聯(lián)于2013年開(kāi)始,先為蕭山等服務(wù)建站,蕭山等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為蕭山企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
在這里包含了javascript代碼。$(“#form”).submit(function(){}- delete_button是form標(biāo)簽的ID,我們通過(guò)element.val()調(diào)用表單輸入框的值。 代碼如下:
代碼如下:
代碼如下:
{
"userDetails":{
"user":{
"name":"Srinivas Tamada",
"email":"srinivas@9lessons.info",
"password":"Srinivas Tamada",
"gender":"male",
"websites":["www.software8.co","www.heatpress123.net","www.0769zzw.com"]
}
}
}
對(duì)特殊字符進(jìn)行編碼,會(huì)把以下字符進(jìn)行編碼
:
代碼如下:
, / ? : @ & = + $ #
jsondata=%7B%22userDetails%22%3A%7B%22user%22%3A%7B%22name%22%3A%22Srinivas%20Tamada%22%2C%22email%22%3A%22srinivas%409lessons.info%22%2C%22password%22%3A%22Srinivas%20Tamada%22%2C%22gender%22%3A%22male%22%2C%22websites%22%3A%5B%22www.9lessons.info%22%2C%22www.egglabs.in%22%2C%22www.fglogin.com%22%5D%7D%7D%7D
這里定義了jquery的ajax請(qǐng)求方法。
代碼如下:
function post_data(url,encodedata, success){
$.ajax({
type:"POST",
url:url,
data :encodedata,
dataType:"json",
restful:true,
contentType: 'application/json',
cache:false,
timeout:20000,
async:true,
beforeSend :function(data) { },
success:function(data){
success.call(this, data);
},
error:function(data){
alert("Error In Connecting");
}
});
}
// 聲明兩個(gè)json數(shù)組
[java] view plain copy print?
JSONArray gResTable = new JSONArray();
JSONArray gCmtTable = new JSONArray();
// 聲明json對(duì)象
ONObject outData = new JSONObject();
//把json數(shù)組加到j(luò)son對(duì)象中
[java] view plain copy print?
outData.put("ResTable", gResTable);
outData.put("CmtTable", gCmtTable);
//此時(shí)創(chuàng)建出來(lái)的如下:jsonData={"ResTable":[],"CmtTable":[]};
把json數(shù)據(jù)加到j(luò)son數(shù)組中
for (int i = 0; i < 2; i ++)
{
JSONObject node = new JSONObject();
node.put("thumb_path", "./Image/" + i +".gif");
node.put("flash_path", "./Image/" + i +".gif");
node.put("desc1", "可疑車輛" + i);
node.put("desc2", "");
node.put("desc3", "");
node.put("desc4", "");
node.put("title", "hello");
node.put("upload_time", (new java.util.Date()).toString());
node.put("uploader", "王二");
gResTable.put(node);
}
for (int i = 0; i < 2; i ++)
{
JSONObject node = new JSONObject();
node.put("logo_path", "./Image/" + i +".gif");
node.put("comment", "hello");
node.put("comment_time", (new java.util.Date()).toString());
node.put("author", "王二");
gCmtTable.put(node);
}
建出來(lái)的json格式如下:
[java] view plain copy print?
/*
jsonData={"ResTable":[
{"upload_time":"Sat May 26 20:16:37 CST 2012","title":"hello","flash_path":"./Image/0.gif","uploader":"王二","thumb_path":"./Image/0.gif","desc3":"","desc4":"","desc1":"可疑車輛0","desc2":""},
{"upload_time":"Sat May 26 20:16:37 CST 2012","title":"hello","flash_path":"./Image/1.gif","uploader":"王二","thumb_path":"./Image/1.gif","desc3":"","desc4":"","desc1":"可疑車輛1","desc2":""}],
"CmtTable":
[{"author":"王二","logo_path":"./Image/0.gif","comment":"hello","comment_time":"Sat May 26 20:16:37 CST 2012"},
{"author":"王二","logo_path":"./Image/1.gif","comment":"hello","comment_time":"Sat May 26 20:16:37 CST 2012"}
]};
*/ 
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流