首頁 >web前端 >js教程 >容 使用JavaScript建構JSON格式字串

容 使用JavaScript建構JSON格式字串

高洛峰
高洛峰原創
2016-11-28 09:50:311199瀏覽

如果你現在正在使用Restful API,並且你需要透過web專案來建立json格式字串的回應,那麼這篇文章將幫助你使用javascript來創建json格式字串。這是非常有用的,我們將透過jQuery外掛$.toJSON把資料物件轉換為json格式。

使用JavaScript建立JSON格式字串

 

JavaScript程式碼:

在這裡包含了javascript程式碼。 $(“#form”).submit(function(){}- delete_button是form標籤的ID,我們透過element.val()呼叫表單輸入框的值。程式碼如下:

<script src="jquery.min.js"></script>
<script src="jquery.json-2.2.js"></script>
<script src="GetPostAjax.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#form").submit(function(e){
e.preventDefault();
var username,email,password,gender;
username=$("#username").val();
email=$("#email").val();
password=$("#username").val();
gender=$("#gender").val();
 
if(username.length>0 && email.length>0 && password.length>0 &&gender.length>0)
{
//Creating Objects
var request = new Object();
var userDetails = new Object();
var user = new Object();
var websites=new Array();
 
user.name=username;
user.email=email;
user.password=password;
user.gender=gender;
 
//Array Push
if(website1.length>0)
websites.push(website1);
if(website2.length>0)
websites.push(website2);
if(website3.length>0)
websites.push(website3);
 
user.websites=websites;
 
userDetails.user = user;
request.userDetails = userDetails;
 
var jsonfy = $.toJSON(request);
// Encodes special characters
var encodedata = &#39;jsondata=&#39;+encodeURIComponent(jsonfy);
 
//Ajax Call
var url=&#39;website API URL&#39;;
post_data(url,encodedata, function(data) {
alert("Success");
});
 
}
 
});
 
});
</script">

 

HTML程式碼:

<form method=&#39;post&#39; action=&#39;&#39; id=&#39;form&#39;>
Name
<input type=&#39;text&#39; name=&#39;username&#39; id=&#39;username&#39;  />
Email
<input type=&#39;text&#39; name=&#39;email&#39; id=&#39;email&#39; />
Password
<input type=&#39;text&#39; name=&#39;password&#39; id=&#39;password&#39; />
Gender
<select name=&#39;gender&#39; id=&#39;gender&#39;><option value=&#39;male&#39;>Male</option><option value=&#39;female&#39;>Female</option></select>
Websites
<input type=&#39;text&#39; id=&#39;website1&#39; />
<input type=&#39;text&#39; id=&#39;website2&#39; />
<input type=&#39;text&#39; id=&#39;website3&#39; />
<input type=&#39;submit&#39; id=&#39;submit&#39;/>
</form>

 

JSON輸出

{
"userDetails":{
"user":{
"name":"Srinivas Tamada",
"email":"srinivas@9lessons.info",
"password":"Srinivas Tamada",
"gender":"male",
"websites":["www.software8.co","www.heatpress123.net","www.0769zzw.com"]
}
}
}
JSON Encoded

 

對特殊字元進行編碼,會將以下字元編碼:, / ? : @ & = + $ #

 

jsondata=%7B%

 

jsondata=%7B%2% 22user%22%3A%7B%22name%22%3A%22Srinivas%20Tamada%22%2C%22email%22%3A%22srinivas%409lessons.info%22%2C%22password%22%3A%22Srinivas% 2C%22gender%22%3A%22male%22%2C%22websites%22%3A%5B%22www.9lessons.info%22%2C%22www.egglabs.in%22%2C%22www.fglogin.com%22www.egglabs.in%22%2C%22www.fglogin.com%22% 5D%7D%7D%7D

GetPostAjax.js

 

這裡定義了jquery的ajax請求方法。
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn