怎么在ajax中使用json传输数据

  介绍

本篇文章给大家分享的是有关怎么在ajax中使用json传输数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

json (JavaScript对象表示法)是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。json采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, c++, c#、Java、JavaScript、Perl、Python等)。这些特性使json成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。

json简单说就是JavaScript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构。

1,对象:对象在js中表示为“{}”括起来的内容,数据结构为{键:值,键:值,……}的键值对的结构,在面向对象的语言中,主要为对象的属性,价值为对应的属性值,所以很容易理解,取值方法为对象。关键获取属性值,这个属性值的类型可以是数字,字符串,数组,对象几种。

2,数组:数组在js中是中括号“[]”括起来的内容,数据结构为(“java",“javascript",“vb",…),取值方式和所有语言中一样,使用索引获取,字段值的类型可以是数字,字符串,数组,对象几种。

经过对象、数组2种结构就可以组合成复杂的数据结构了。

使用JSON前需要先的导入JSON。jar包

怎么在ajax中使用json传输数据

<强>传输单个对象:

新建一个servlet

package  com.itnba.maya.a;   import  java.io.IOException;   import  javax.servlet.ServletException;   import  javax.servlet.annotation.WebServlet;   import  javax.servlet.http.HttpServlet;   import  javax.servlet.http.HttpServletRequest;   import  javax.servlet.http.HttpServletResponse;   import  org.json.JSONObject;/* *   ,* Servlet  implementation  class  C   ,*/@WebServlet (“/C")   public  class  C  extends  HttpServlet  {   ,private  static  final  long  serialVersionUID =, 1 l;/* *   *,才能@see  HttpServlet # HttpServlet ()   ,*/,public  C (), {   超级才能();//才能,TODO  Auto-generated  constructor 存根   ,}/* *   *,才能@see  HttpServlet # doGet (HttpServletRequest 请求,HttpServletResponse 响应)   ,*/,protected  void  doGet (HttpServletRequest 请求,,HttpServletResponse 响应),throws  ServletException, IOException  {   request.setCharacterEncoding才能(“utf-8");   response.setCharacterEncoding才能(“utf-8");//模才能拟从数据库中查处   Dog 才能;=new 狗();   a.setName才能(“小黄“);   a.setAge才能(5);   a.setZl才能(“哈士奇“);   JSONObject 才能obj=new  JSONObject ();   obj.put才能(“name",, a.getName ());   obj.put才能(“age",, a.getAge ());   obj.put才能(“zl",, a.getZl ());   JSONObject 才能bb=new  JSONObject ();   bb.put才能(“obj",, obj);   response.getWriter才能().append (bb.toString ());   ,}/* *   *,才能@see  HttpServlet # doPost (HttpServletRequest 请求,HttpServletResponse 响应)   ,*/,protected  void  doPost (HttpServletRequest 请求,,HttpServletResponse 响应),throws  ServletException, IOException  {//才能,TODO  Auto-generated  method 存根   ,,doGet(请求,响应);   ,}   }

效果如下:

怎么在ajax中使用json传输数据

<强> jsp页面

& lt; % @  page 语言=癹ava", contentType=皌ext/html;, charset=utf-8"   ,pageEncoding=皍tf-8" %比;   & lt; ! DOCTYPE  html  PUBLIC “-//W3C//DTD  html  4.01,过渡//EN",“http://www.w3.org/TR/html4/loose.dtd"比;   & lt; html>   & lt; head>   & lt; meta  http-equiv=癈ontent-Type",内容=皌ext/html;, charset=utf-8"比;   & lt; title> Insert  title  here   & lt; script 类型=拔谋?javascript", src=癹s/jquery-1.11.1.min.js"祝辞& lt;/script>   & lt; script 类型=拔谋?javascript"比;   $(文档)时函数(){   ,$ (“# k") .click(函数(){   . ajax({美元才能   ,,url:“C"   ,,数据:{},   ,,类型:“POST",   ,,数据类型:“JSON"   ,才能成功:函数(httpdata) {   ,,,(“# x") .append美元(“& lt; li>“+ httpdata.obj.name +“& lt;/li>“);   ,,,(“# x") .append美元(“& lt; li>“+ httpdata.obj.age +“& lt;/li>“);   ,,,(“# x") .append美元(“& lt; li>“+ httpdata.obj.zl +“& lt;/li>“)   ,,}   })才能   ,});   });   & lt;/script>   & lt;/head>   & lt; body>   & lt; span  id=発"祝辞查看& lt;/span>   & lt; h2>   & lt; ul  id=皒"祝辞   & lt;/ul> & lt;/h2>   & lt;/body>   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在ajax中使用json传输数据