Ajax uses json for data transmission and ajaxjson for data transmission

Source: Internet
Author: User

Ajax uses json for data transmission and ajaxjson for data transmission

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy to read and write, and easy to parse and generate by machines (generally used to increase the network transmission rate ).

Json is simply an object and an array in javascript. Therefore, the two structures are objects and arrays. These two structures can represent various complex structures.

1. Object: Content enclosed by the object represented as "{}" in js. The data structure is {key: value, key: value ,...} the structure of the key-value pair. In the object-oriented language, the key is the object attribute, and the value is the corresponding attribute value, so it is easy to understand that the value method is the object. key, which can be a number, String, array, or object.

2. array: The array is enclosed by brackets "[]" in js. The data structure is ["java", "javascript", "vb",...], the value can be obtained using indexes in the same way as in all languages. Field values can be of the following types: Numbers, strings, arrays, and objects.

After the object and array structures, you can combine them into complex data structures.

Import the JSON. jar package before using json

Transmit a single object:

Create a 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 = 1L; /*** @ see HttpServlet # HttpServlet () */public C () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); // simulates Dog a = new Dog ();. setName ("Xiao Huang");. setAge (5);. setZl ("husky"); JSONObject obj = new JSONObject (); obj. put ("name",. getName (); obj. put ("age",. getAge (); obj. put ("zl",. getZl (); JSONObject bb = new JSONObject (); bb. put ("obj", obj); response. getWriter (). append (bb. toString ();}/*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet (request, response );}}

The effect is as follows:

Jsp page

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

The effect is as follows:

Transmission set or array:

Servlet:

Package com. itnba. maya. a; import java. io. IOException; import java. util. arrayList; 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. JSONArray; import org. json. JSONObject;/*** Servlet implementation class D */@ WebServlet ("/D") public class D extends HttpServlet {private static final long serialVersionUID = 1L; /*** @ see HttpServlet # HttpServlet () */public D () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); // simulates Dog a1 = new Dog (), a1.setName ("yellow"), and a1.setAge (5) from the database ); a1.setZl ("husky"); Dog a2 = new Dog (); a2.setName ("中 "); a2.setAge (6); a2.setZl ("Teddy "); dog a3 = new Dog (); a3.setName ("rhubarb"); a3.setAge (7); a3.setZl ("jingba "); arrayList <Dog> list = new ArrayList <Dog> (); list. add (a1); list. add (a2); list. add (a3); JSONArray arr = new JSONArray (); // traverses the set for (Dog d: list) {JSONObject obj = new JSONObject (); obj. put ("name", d. getName (); obj. put ("age", d. getAge (); obj. put ("zl", d. getZl (); arr. put (obj);} response. getWriter (). append (arr. toString ();}/*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet (request, response );}}

The effect is as follows:

Jsp page:

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN "" http://www.w3.org/TR/html4/loose.dtd "> <Html> 

The effect is as follows:

 

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.