JSON and Ajax induction

**

json definition

**
json is composed of braces, and then there are pairs.
And each key by quotation quotes. Separated by colons between the key and values. Then separated by a comma between each set of key-value pairs.

var json = {
“key” : value ,
“key2” : value2
};

Value may be a string, number, array type, json objects. json array.

var jsonObj = {
					"key1" : 12,		//number
					"key2" : "abc",  //String
					"key3" : ["123","bda",true],		//数组类型
					"key4" : { // json套json	json对象
						"key4_1" : 41,
						"key4_2" : "42"
					},
					"key5" : [{						//json数组
						"key5_1_1" : 511,
						"key5_1_2" : "512"
					},{
						"key5_2_1" : 521,
						"key5_2_2" : "522"
					}]
			};

**

Two common methods Json

**
the JSON.stringify () to the object json json string converted into
the JSON.parse () is converted into the string json json objects
**

JSON use in java

**

GSON的使用
		Gson gson =  new Gson();
		gson.toJson();   //把任意数据转换成Json字符串
		gson.fromJson(json字符串, java对象.class) //把json字符串转换成java对象	
				但是如果是集合 就不能传入集合中封装的对象.class (list 和map都是一样的~)
				解决方法 :
				(1)我们需要继承Gson里面的TypeToken 范型为集合(集合的范型为你的类),然后将第二个参数改成你的新的对象 并且调用getType方法 
				(2)可以直接new TypeToken 范型为集合(集合的范型为你的类),然后将第二个参数改成你的新的对象 并且调用getType方法 

**

Ajax

**
basic concepts: asynchronous request to initiate a partial update

原生的方法:
1new XMLHttpRequest()
2调用open方法 填入 请求方式 url 第三个参数代表是否异步 是就写true
3调用 send()方法
4onreadystatechange=function(){
	 if (request.readyState ==4 && request.status == 200){
	 		调用responseText
	}
}
但是一般第四步写在第三步前头  要不有可能会有响应丢失的情况~

Here Insert Picture Description

jquery
$.ajax方法
url						请求的地址
type 					请求的方式GET或POST
data					请求的参数。有两种格式,name=value&name=value	或 {key:value,key:value}
success					成功的回调函数
dataType				返回的数据类型。常用的类型是:text、json、xml

$.get方法和$.post方法
url					请求的地址 
data					请求的参数
callback				成功的回调函数
type					返回的数据类型。常用的类型是:text、json、

$.getJSON方法
url					请求的地址
data					请求的参数
callback				请求成功回调函数

**

Serialization form parameter

**

	$("表单").serialize()          直接返回表单所有参数并且用= &连接好的
Published 63 original articles · won praise 44 · views 6266

Guess you like

Origin blog.csdn.net/weixin_40695328/article/details/89648468