JSON data conversion and parsing


JSON data conversion and parsing

content review

ajax asynchronous request

格式:
$.ajax({
url:"请求地址",
data:{},
type:"post/get",
async:true/false,
dataType:"text/json",
success:function(obj){ },
error:function(){}
})
$.post/get(
"请求地址",
{key:value,key:value}
function(obj){}
"text/json"
)

json

① json 对象
格式:{key:value,key:value}
json 对象.key --- 获取 value 值
② json 数组
格式:[元素,元素,元素,······]
json 数组[索引] -- 获取元素
③ 对象数组混合格式
格式:{[]} --- 对象数组
json 对象.key[索引] -- 获取对象数组中的值
[{}] --- 数组对象
json 数组[索引].key -- 获取数组对象中的值

Jackson tools

Jackson 工具需要 3 个依赖包:jackson-databind,jackson-core,jacksonannotations;
① 对象转成 json 字符串
a. 创建 ObjectMapper 对象
b. 使用 ObjectMapper 对象提供的 writeVlaueAsString(object)
c. 将 json 字符响应给页面

Convert the Map collection into a JSON string

Servlet

insert image description here

The result of the servlet response

insert image description here

dao layer data encapsulation

insert image description here

jsp page

insert image description here

Convert List collection to JSON string

servlet class

insert image description here

service layer

insert image description here
insert image description here

dao layer

insert image description here

insert image description here

jsp page

insert image description here

insert image description here

Ajax asynchronous and synchronous

asynchronous concept

在同一个 jsp 页面中,可以存在多个 ajax,所有的 ajax 在进行请求发送时,没有固定
的先后顺序,并发执行的;

synchronization concept

多个请求有明显的先后顺序,按照 jsp 页面中书写的先后顺序执行;

The difference between asynchronous and synchronous

在异步请求发送的时候,如果在某一个请求中需要另一个一个请求中的数据,此时不能保证一
定能够获取;
同步请求时,后一个请求一定能够获取前一个请求中的数据;
同步和异步都是使用请求中的属性:async,将其值设置为 true 时,表示异步;设置为
false 表示同步;

Asynchronous request case

jsp page

insert image description here

Results of the

insert image description here

insert image description here

summary

It can be seen from the execution results that asynchronous requests are not executed in the order in which they were written; if you want the requests to be executed in the order in which they are written, you need to set the request to be synchronous;

synchronous request

insert image description here

time formatting

old time api formatting

项目中用来表示日期类型如果是 java.util.Date 类型,那么在进行时间显示时需要使用
SimpleDateFormat 进行时间的格式化;
① 创建 SimpleDateFormat 对象
② 使用 format 方法进行格式化
同理有的时候也需要进行字符串解析为时间格式,使用 parse 方法

Tools for formatting and parsing

insert image description here

使用工具类进行格式化,需要在查询或者添加数据时对数据先进行格式化或者或者解析,再进
行数据操作;使用起来相对麻烦;
建议使用 JSTL 提供的格式化工具进行时间格式化;

JSTL time formatting

Introducing JSTL's core tag library and formatting tools
insert image description here

JSTL uses

insert image description here

servlet class
insert image description here

dao layer

insert image description here

Guess you like

Origin blog.csdn.net/qq_54525448/article/details/129338250