非常教程

JSON教程

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"} 
 ]}; 
*/ 
JSON

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等),易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。从Web API和服务端编程语言到NoSQL数据库和客户端框架,都有JSON的身影,在不同平台间传递数据方面,JSON已成为XML强有力的替代者。

主页 http://www.json.org/json-zh.htmljson
源码 https://github.com/topics/json

JSON目录

1. JSON教程
2.JSON 教程
3.JSON 基础
4.json格式
5.JSON 示例
6.JSON解析
7.JSON遍历
8.JSON调用
9.JSON转换
10.JSON获取
11.JSON字符串
12.JSON数组
13.JavaScript 教程
14.JSON 教程
15.JavaScript 注释
16.JavaScript 语句
17.JavaScript 输出
18.JavaScript 用法
19.JavaScript 简介
20.JavaScript switch 语句
21.JavaScript If…Else 语句
22.JavaScript 比较和逻辑运算符
23.JavaScript 运算符
24.JavaScript 函数
25.JavaScript 对象
26.JavaScript 数据类型
27.JavaScript 变量
28.JavaScript for 循环
29.JavaScript HTML DOM 事件
30.JavaScript HTML DOM 改变 CSS
31.JavaScript HTML DOM 改变 HTML 内容
32.JavaScript HTML DOM
33.JavaScript 表单验证
34.JavaScript 错误 – Throw、Try 和 Catch
35.JavaScript break 和 continue 语句
36.JavaScript while 循环
37.JavaScript HTML DOM 元素 (节点)
38.JavaScript Number 对象
39.JavaScript 对象
40.JavaScript Date(日期)对象
41.JavaScript 字符串(String)对象
42.JavaScript Window History
43.JavaScript Window Location
44.JavaScript Window Screen
45.JavaScript Window
46.JavaScript RegExp 对象
47.JavaScript Math(算数)对象
48.JavaScript Boolean(布尔)对象
49.JavaScript Array(数组)对象
50.JavaScript 实例
51.JavaScript 测试 Prototype
52.JavaScript 测试 jQuery
53.JavaScript 库
54.JavaScript Cookie
55.JavaScript 计时事件
56.JavaScript 弹窗
57.JavaScript Window Navigator
58.JavaScript HTML DOM 实例
59.JavaScript 对象实例
60.JavaScript 总结
61.JavaScript 浏览器对象实例
62.JSON 语法
63.JSON 使用
64.JSON 简介
65.JavaScript 保留关键字
66.JavaScript 字符串
67.JavaScript 事件
68.JavaScript 作用域
69.JavaScript JSON
70.JavaScript HTML DOM EventListener
71.JSONP 教程
72.javascript:void(0) 含义
73.JavaScript 语法
74.JavaScript 调试
75.JavaScript 闭包
76.JavaScript 函数调用
77.JavaScript 函数参数
78.JavaScript 函数定义
79.JavaScript 类型转换
80.JavaScript typeof, null, 和 undefined
81.JavaScript 正则表达式
82.JavaScript JSON.stringify()
83.JavaScript JSON.parse()
84.JavaScript 代码规范
85.JavaScript 使用误区
86.JavaScript 严格模式(use strict)
87.JavaScript 变量提升
88.JavaScript 验证 API
89.JavaScript 表单
90.JSON.stringify()
91.JSON.parse()
92.JSON 数组
93.JSON 对象
94.JavaScript HTML DOM 节点列表
95.JavaScript HTML DOM 集合(Collection)
96.JavaScript let 和 const
97.JavaScript this 关键字
98.JavaScript prototype(原型对象)
99.JSON vs XML
100.JavaScript Promise