JSON 教程
- 第1节:JSON 简介
- 第2节:JSON 语法
- 第3节:JSON 使用
- 第4节:JSONP 教程
- 第5节:JSON 数据类型
- 第6节:JSON 对象
- 第7节:JSON 模式
- 第8节:JSON 与 XML 对比
- 第9节:JSON 基础结构
- 第10节:JSON 数据格式
- 第11节:JSON 数据解析的方法
- 第12节:JSON 数据解析过程详解
- 第13节:JSON 解析URL参数
- 第14节:JSON 元素的添加删除
- 第15节:JSON 数据解析的3种方式
- 第16节:JavaScript 解析Json字符串的性能比较分析代码
- 第17节:JavaScript解析json格式数据简单示例
- 第18节:JSON数据遍历之for-in
- 第19节:jQuery 遍历Json的实现代码
- 第20节:JS遍历Json字符串
- 第21节:直接在JS里创建JSON数据然后遍历使用
- 第22节:JS循环遍历JSON数据的方法
- 第23节:js 遍历json返回的map内容示例代码
- 第24节:JSP+jquery使用ajax方式调用json的实现方法
- 第25节:jquery中的ajax方法怎样通过JSONP进行远程调用
- 第26节:jquery ajax jsonp跨域调用实例代码
- 第27节:jQuery插件jQuery-JSONP开发ajax调用使用注意事项
- 第28节:json的键名为数字时的调用方式
- 第29节:php跨域调用json的例子
- 第30节:jquery中ajax调用json数据的使用说明
- 第31节:ASP.NET(AJAX+JSON)实现对象调用
- 第32节:JavaScript转换与解析JSON方法实例详解
- 第33节:将JavaScript的jQuery库中表单转化为JSON对象的方法
- 第34节:javascript中JSON对象与JSON字符串相互转换实例
- 第35节:JavaScript中字符串(string)转json的方法
- 第36节:JavaScript将XML转成JSON的方法
- 第37节:java与javascript之间json格式数据互转介绍
- 第38节:转义字符(\)对JavaScript中JSON.parse的影响概述
- 第39节:JavaScript中json对象和string对象之间相互转化
- 第40节:javascript中将Object转换为String函数代码 (json str)
- 第41节:转换json格式的日期为Javascript对象的函数
- 第42节:AJAX跨域请求获取JSON数据
- 第43节:JS根据key值获取URL中的参数值
- 第44节:jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表
- 第45节:js获取json元素数量的方法
- 第46节:JSONP获取Twitter和Facebook文章数的具体步骤
- 第47节:js获取通过ajax返回的map型的JSONArray的方法
- 第48节:JSP中获取ExtJS.Ajax前台传递的JSON数据实现过程
- 第49节:jQuery中使用Ajax获取JSON格式数据示例代码
- 第50节:JQuery 获取json数据$.getJSON方法的实例代码
- 第51节:javascript中JSON对象与JSON字符串相互转换实例
- 第52节:JavaScript中字符串(string)转json的方法
- 第53节:IE6/IE7中JavaScript json提示缺少标识符、字符串或数字问题处理
- 第54节:使用JavaScript构建JSON格式字符串实现步骤
- 第55节:JavaScript 解析Json字符串的性能比较分析代码
- 第56节:javascript巧用eval函数组装表单输入项为json对象的方法
- 第57节:用javascript对一个json数组深度赋值示例
- 第58节:解析javascript 数组(以及json元素的添加删除)
- 第59节:js数组如何添加json数据
- 第60节:js/jquery解析json和数组格式的方法详解
使用JavaScript构建JSON格式字符串实现步骤
如果你现在正在使用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 = 'jsondata='+encodeURIComponent(jsonfy);
//Ajax Call
var url='website API URL';
post_data(url,encodedata, function(data) {
alert("Success");
});
}
});
});
</script">
HTML代码
代码如下:
<form method='post' action='' id='form'>
Name
<input type='text' name='username' id='username' />
Email
<input type='text' name='email' id='email' />
Password
<input type='text' name='password' id='password' />
Gender
<select name='gender' id='gender'><option value='male'>Male</option><option value='female'>Female</option></select>
Websites
<input type='text' id='website1' />
<input type='text' id='website2' />
<input type='text' id='website3' />
<input type='submit' id='submit'/>
</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%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
GetPostAjax.js
这里定义了jquery的ajax请求方法。代码如下:
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");
}
});
}
java创建json对象
// 声明两个json数组
[java] view plain copy print?
JSONArray gResTable = new JSONArray();
JSONArray gCmtTable = new JSONArray();
// 声明json对象
ONObject outData = new JSONObject();
//把json数组加到json对象中
[java] view plain copy print?
outData.put("ResTable", gResTable);
outData.put("CmtTable", gCmtTable);
//此时创建出来的如下:jsonData={"ResTable":[],"CmtTable":[]};
把json数据加到json数组中
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);
}
建出来的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"}
]};
*/