【WEEK3】 【DAY4】JSON Interaction Handling Part Three【English Version】
2024.3.14 Thursday
Following the previous article 【WEEK3】 【DAY3】JSON Interaction Handling Part Two【English Version】
Contents
- 6.7. Writing Abstract Classes
- 6.7.1. Reason
- 6.7.2. Create JsonUtils.java
- 6.7.3. Add a method json6 in UserController to verify the abstract class can be called
- 6.7.4. Add a method json7 in UserController to verify the abstract class is reusable
- 6.7.5. Run
- 6.8. FastJson
- 6.8.1. Overview
- 6.8.1.1 Introduction to fastjson.jar
- 6.8.1.2. Three main classes of Fastjson
- 1. JSONObject represents a JSON object
- 2. JSONArray represents a JSON object array
- 3. JSON represents the conversion between JSONObject and JSONArray
- 6.8.2. Import dependencies in pom.xml
- 6.8.3. Code Testing
- 6.8.3.1.Modify the method json7 in UserController
- 6.8.3.2. Create a new FastJsonDemo.java
- 6.8.4. Tips
6.7. Writing Abstract Classes
6.7.1. Reason
If the above functions are frequently used, it can be cumbersome to write them each time, so we can encapsulate these codes into a utility class.
6.7.2. Create JsonUtils.java
package P14.utils;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;import java.text.SimpleDateFormat;public class JsonUtils {// This method overloads getJson, so there is no need to rewrite the specific code; simply return the default value.public static String getJson(Object object) {return getJson(object, "yyyy-MM-dd HH:mm:ss");}public static String getJson(Object object, String dateFormat) {ObjectMapper mapper = new ObjectMapper();// Do not use time difference methodmapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);// Custom date format objectSimpleDateFormat sdf = new SimpleDateFormat(dateFormat);// Specify date formatmapper.setDateFormat(sdf);try {return mapper.writeValueAsString(object);} catch (JsonProcessingException e) {e.printStackTrace();}return null;}
}
6.7.3. Add a method json6 in UserController to verify the abstract class can be called
@RequestMapping("/j6_utils")public String json6(){Date date = new Date();return JsonUtils.getJson(date, "yyyy-MM-dd HH:mm:ss");
// HH is for 24-hour format, hh is for 12-hour format
// return JsonUtils.getJson(date); is also possible}
6.7.4. Add a method json7 in UserController to verify the abstract class is reusable
@RequestMapping("/j7_utils_j2")public String json7() throws JsonProcessingException {// Create a collectionList<User> userList = new ArrayList<>();User user1 = new User("Zhang San", 11, "female");User user2 = new User("Li Si", 11, "male");User user3 = new User("Wang Wu", 11, "female");// Add users to the collectionuserList.add(user1);userList.add(user2);userList.add(user3);return JsonUtils.getJson(userList);}
6.7.5. Run
http://localhost:8080/springmvc_05_json_war_exploded//j6_utils
http://localhost:8080/springmvc_05_json_war_exploded//j7_utils_j2
The result obtained by running method json7 is exactly the same as method json2.
6.8. FastJson
6.8.1. Overview
6.8.1.1 Introduction to fastjson.jar
fastjson.jar
is a package developed by Alibaba specifically for Java development, which can conveniently implement the conversion between JSON objects and JavaBean objects, the conversion between JavaBean objects and JSON strings, and the conversion between JSON objects and JSON strings. There are many methods to implement JSON conversion, and the final results are all the same.
6.8.1.2. Three main classes of Fastjson
1. JSONObject represents a JSON object
- JSONObject implements the Map interface, suggesting that JSONObject’s underlying operations are implemented by Map.
- JSONObject corresponds to a JSON object, through various forms of get() methods you can get data from a JSON object, and also use methods such as size(), isEmpty() to get the number of “key-value” pairs and determine whether it is empty.
2. JSONArray represents a JSON object array
- Internally it uses methods from the List interface to complete operations.
3. JSON represents the conversion between JSONObject and JSONArray
- Analysis and usage of JSON class source code.
- Carefully observing these methods, the main purpose is to implement the conversion between JSON objects, JSON object arrays, JavaBean objects, and JSON strings.
6.8.2. Import dependencies in pom.xml
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.60</version></dependency>
6.8.3. Code Testing
6.8.3.1.Modify the method json7 in UserController
Change it to use fastjson as the return value of the abstract class
@RequestMapping("/j7_utils_j2")public String json7() throws JsonProcessingException {// Create a collectionList<User> userList = new ArrayList<>();User user1 = new User("Zhang San", 11, "female");User user2 = new User("Li Si", 11, "male");User user3 = new User("Wang Wu", 11, "female");// Add users to the collectionuserList.add(user1);userList.add(user2);userList.add(user3);// return JsonUtils.getJson(userList);
// Parsing with fastjson is as followsString str = JSON.toJSONString(userList);return str;}
- Before running, remember to add the fastjson dependency package in Project Structure
- Otherwise:
- Execution
http://localhost:8080/springmvc_05_json_war_exploded/j7_utils_j2
After using fastjson, the result of json7 is still exactly the same as method json2 (the same as before modifying json7).
6.8.3.2. Create a new FastJsonDemo.java
package P14.controller;import P14.project.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RestController
public class FastJsonDemo {@RequestMapping("/fj")public String fastjson(){// Create an objectUser user1 = new User("Zhang San", 3, "male");User user2 = new User("Li Si", 3, "male");User user3 = new User("Wang Wu", 3, "male");User user4 = new User("Zhao Liu", 3, "male");List<User> list = new ArrayList<User>();list.add(user1);list.add(user2);list.add(user3);list.add(user4);System.out.println("*******Java Object to JSON String*******");String str1 = JSON.toJSONString(list);System.out.println("JSON.toJSONString(list)==>" + str1);String str2 = JSON.toJSONString(user1);System.out.println("JSON.toJSONString(user1)==>" + str2);System.out.println("\n****** JSON String to Java Object*******");User jp_user1 = JSON.parseObject(str2, User.class);System.out.println("JSON.parseObject(str2,User.class)==>" + jp_user1);System.out.println("\n****** Java Object to JSON Object ******");JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);System.out.println("(JSONObject) JSON.toJSON(user2)==>" + jsonObject1.getString("name"));System.out.println("\n****** JSON Object to Java Object ******");User to_java_user = JSON.toJavaObject(jsonObject1, User.class);System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);return str1;}
}
- Run (Output a JSON string from a Java object as the page response)
http://localhost:8080/springmvc_05_json_war_exploded/fj
6.8.4. Tips
- For such utility classes, it is enough for us to know how to use them. When using them, we should look for the corresponding implementation based on the specific business needs, just like the commons-io toolkit we used before; just use it!
- JSON is very important in data transmission; it is essential to learn how to use it.
相关文章:
【WEEK3】 【DAY4】JSON Interaction Handling Part Three【English Version】
2024.3.14 Thursday Following the previous article 【WEEK3】 【DAY3】JSON Interaction Handling Part Two【English Version】 Contents 6.7. Writing Abstract Classes6.7.1. Reason6.7.2. Create JsonUtils.java6.7.3. Add a method json6 in UserController to verify…...
蓝桥杯物联网竞赛_STM32L071_12_按键中断与串口中断
按键中断: 将按键配置成GPIO_EXTI中断即外部中断 模式有三种上升沿,下降沿,上升沿和下降沿都会中断 external -> 外部的 interrupt -> 打断 trigger -> 触发 detection -> 探测 NVIC中将中断线ENABLE 找接口函数 在接口函数中写…...
Java安全 反序列化(1) URLDNS链原理分析
Java安全 反序列化(1) URLDNS链原理分析 文章目录 Java安全 反序列化(1) URLDNS链原理分析前置知识应用分析payload1.新建HashMap类2.新建URL类3.获取URL 的 Class对象4.通过反射访问URL内部变量5.通过反射为URL中类赋值6.调用HashMap#put方法传入key和value7.再次通过反射为UR…...
电脑插上网线之后仍然没网络怎么办?
前言 有小伙伴在使用Windows系统的时候,经常会遇到电脑没网络,但又不知道具体怎么调整才好。 本篇内容适合插网线和使用Wi-Fi的小伙伴,文章本质上是重置电脑的网络设置。 注意事项:网络重置操作会让已连接过的wifi密码丢失&…...
easyexcel读和写excel
请直接看原文: JAVA使用easyexcel操作Excel-CSDN博客 -------------------------------------------------------------------------------------------------------------------------------- 之前写过一篇《JAVA操作Excel》,介绍了jxl和poi读写Excel的实现&am…...
路由器级联
目录 一、实现功能二、实现步骤2.1 接线步骤 三、效果3.1 常规连接3.2 路由器级联 一、实现功能 主路由器:可有WiFi功能,LAN口下接各设备,并接一个辅路由器辅路由器:开启WiFi功能,有线或无线下接各设备功能࿱…...
CentOS7使用Docker部署.net Webapi
1 准备WebApi项目 对于已存在的WebApi项目,需要添加Docker支持; 编码时,先设置好项目需要的端口号:program.cs中,app.Run("http://*:8000");设置端口为:8000在VS中,选中项目…...
Windows程序员用MAC:初始设置(用起来像win一些)
初级代码游戏的专栏介绍与文章目录-CSDN博客 初用Mac会有很多不习惯,特别是鼠标滚轮的滚动方向和windows是反的,还好是通过设置改变的。下面是我自己的设置。 目录 一、显示器设置 二、屏保、时钟、触发角 三、程序坞与菜单栏 四、安全性与隐私 五…...
基于深度学习YOLOv8+Pyqt5的工地安全帽头盔佩戴检测识别系统(源码+跑通说明文件)
wx供重浩:创享日记 对话框发送:318安全帽 获取完整源码源文件7000张已标注的数据集训练好的模型配置说明文件 可有偿59yuan一对一远程操作配置环境跑通程序 效果展示(图片检测批量检测视频检测摄像头检测) 基于深度学习YOLOv8Pyqt…...
csv编辑器是干什么的?
csv编辑器是一种用于编写、编辑和管理文本文件的工具。适用于 JetBrains IDE 系列的 CSV 编辑器插件,此插件将 CSV(逗号分隔值)作为一种语言引入 Jetbrains IDE,其中包含语法定义、结构化语言元素和关联的文件类型 (.c…...
计算机网络——物理层(奈氏准则和香农定理)
计算机网络——物理层(奈氏准则和香农定理) 失真码间串扰奈氏准则(奈奎斯特定理)极限数据率 噪声信噪比香农定理奈氏准则和香农定理的区别 前面我们已经了解一些数据通信的基本知识,没有看过上一篇得小伙伴可以点击这里…...
XML语言的学习记录3-解析
学习笔记: 一、 解析XML文档: 1.使用 XMLHttpRequest 对象,将xml文档放入XML DOM对象中: xmlhttpnew XMLHttpRequest(); xmlhttp.open("GET","books.xml",false); xmlhttp.send(); xmlDocxmlhttp.response…...
【Linux】cat vim 命令存在着什么区别?
Linux 中的 cat 命令和 vim 命令之间存在一些显著的区别! cat 命令 首先,cat命令主要用于连接并显示文件的内容。它的原含义是“连接(concatenate)”,可以将多个文件的内容连接起来,并输出到标准输出流中&…...
MeterSphere和Jmeter使用总结
一、MeterSphere 介绍 MeterSphere 是⼀站式开源持续测试平台,涵盖测试跟踪、接⼝测试、UI 测试和性能测试等,全 ⾯兼容 JMeter、Selenium 等主流开源标准,能够有效助⼒开发和测试团队在线共享协作,实现端到 端的测试管理跟踪…...
学习笔记Day8:GEO数据挖掘-基因表达芯片
GEO数据挖掘 数据库:GEO、NHANCE、TCGA、ICGC、CCLE、SEER等 数据类型:基因表达芯片、转录组、单细胞、突变、甲基化、拷贝数变异等等 常见图表 表达矩阵 一行为一个基因,一列为一个样本,内容是基因表达量。 热图 输入数据…...
如何将大华dav视频转mp4?一键无损清晰转换~
Digital Audio Video(DAV)文件格式源于数字监控领域,旨在解决视频监控数据的存储和回放问题。随着数字监控技术的发展,DAV格式逐渐成为监控设备记录视频的标准格式,广泛应用于安防系统、摄像头监控等场景。 MP4文件格式…...
数字化转型导师坚鹏:人工智能在金融机构数字化转型中的应用
人工智能在金融机构数字化转型中的应用 课程背景: 金融机构数字化转型离不开人工智能,在金融机构数字化转型中,人工智能起到至关重要的作用,很多机构存在以下问题: 不清楚人工智能产业对我们有什么影响?…...
部署Zabbix Agents添加使能监测服务器_Windows平台_MSI/Archive模式
Windows平台 一、从MSI安装Windows代理,添加Windows Servers/PC 概述 可以从Windows MSI安装包(32位或64位) 安装Zabbix agent 32位包不能安装在64位Windows中 所有软件包都支持TLS,配置TLS可选 支持UI和命令行的安装。 1、下载Agent代理程序,使用Agent2升级版,官网链接如…...
十一 超级数据查看器 讲解稿 详情6 导出功能
十一 超级数据查看器 讲解稿 详情6 导出功能 点击此处 以新页面 打开B站 播放当前教学视频 app下载地址 百度手机助手 下载地址4 讲解稿全文: 导出功能讲解 这节课我们讲解一下导出功能, 导出功能,可以将当前查到的信息导出成E…...
java遍历文件目录去除中文文件名
文章目录 一、原始需求二、maven依赖三、核心代码四、运行结果 一、原始需求 需要遍历文件目录及其子目录,找出包含中文字符的文件名,将中文字符去除。 二、maven依赖 pom.xml <dependency><groupId>org.apache.commons</groupId>&…...
LeetCode Python - 61. 旋转链表
目录 题目描述解法运行结果 题目描述 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。 示例 1: 输入:head [1,2,3,4,5], k 2 输出:[4,5,1,2,3] 示例 2: 输入:head…...
k8s client-java创建pod常见问题
1.配置Kubernetes连接: 使用Kubernetes的配置文件(通常位于~/.kube/config)或直接使用API服务器地址、认证信息等来配置客户端。 配置通常有两种模式,安全和不安全的配置模式,对认证要求严格就选用安全模式ÿ…...
C++——字符串、读写文件、结构体、枚举
个人简介 👀个人主页: 前端杂货铺 🙋♂️学习方向: 主攻前端方向,正逐渐往全干发展 📃个人状态: 研发工程师,现效力于中国工业软件事业 🚀人生格言: 积跬步…...
vscode 运行 java 项目之解决“Build failed, do you want to continue”的问题
Visual Studio Code运行 java 起来似乎比 IDEA 更轻量、比 eclipse 更友好,是不可多得的现代编译法宝。 安装好官方推荐的 java 扩展包后,就可以运行 java 代码了。功能 比 code runner 强,支持 gradle、maven、普通java项目,运行…...
yocto编译测试
源码下载 git clone -b gatesgarth git://git.yoctoproject.org/poky lkmaolkmao-virtual-machine:~/yocto$ git clone -b gatesgarth git://git.yoctoproject.org/poky Cloning into poky... remote: Enumerating objects: 640690, done. remote: Counting objects: 100% (13…...
rsync+inotify-tools文件传输
目录 rsync rsync概述 rsync优缺点 rsync参数 rsync命令 rsync同步源 linux 安装rsync 安装rsync服务端 安装rsync客户端 windows 安装rsync rsync实践 inotify-tools inotify-tools概述 innotify-tools 命令、参数、事件 rsync客户端安装inotify-tools innotif…...
UGUI界面性能优化3-合理规划界面层级结构
在Unity中,UGUI(Unity GUI)是一种用于创建用户界面的工具。合理规划界面层级结构对于开发一个可维护和易于使用的界面非常重要。以下是一种合理的UGUI界面层级结构规划方式: Canvas(画布):Canva…...
《论文阅读》EmpDG:多分辨率交互式移情对话生成 COLING 2020
《论文阅读》EmpDG:多分辨率交互式移情对话生成 COLING 2020 前言简介模型架构共情生成器交互鉴别器损失函数前言 亲身阅读感受分享,细节画图解释,再也不用担心看不懂论文啦~ 无抄袭,无复制,纯手工敲击键盘~ 今天为大家带来的是《EmpDG: Multi-resolution Interactive E…...
C语言calloc函数的特点,效率低。但是进行初始化操作
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h> #include<string.h> #include<errno.h> #include<stdio.h> int main() { int *p (int *)calloc(10,sizeof(int)); //初始化,效率低,然而malloc函数相反…...
项目中遇到的sql问题记录
有一张表,表结构及数据如下: INSERT INTO test.test_approve(approve_no, tra_date, tablename, part_dt) VALUES (approve001, 2021-02-18 00:00:00, tableA, 2024-03-18); INSERT INTO test.test_approve(approve_no, tra_date, tablename, part_dt) …...
我国婚纱网站建设的现状/关键词优化推广公司排名
为什么80%的码农都做不了架构师?>>> 国内的应用开发者每天都在辛辛苦苦写完代码处理了无数种分辨率、兼容性问题之后,还要面对可能要发布到国内应用市场,各种应用中心、下载中心、论坛等各种线下推广的巨大悲剧情况,再…...
网站服务器拒绝连接/蜗牛精灵seo
参与活动主题 《人月神话(40周年纪念版)再版 扒一扒你遇到过最NB开发项目》有奖活动,三重惊喜,有奖试读&作者互动关注有礼! 为什么是《人月神话》? 这本书在业界真的很有名,几乎无人不知&am…...
连云港最新疫情/南京百度seo公司
很多事情做着做着就偏了,半途而废了。因为周计划没做,所以随波逐流了。因此,在每周日定下周计划,至于太长的规划,不要想太多,太大的目标容易畏惧和自暴自弃。 周一: GDAL源码剖析:…...
wordpress知更鸟配置/网站推广seo
目录 一、java反射机制概述 1.是什么? 2.反射中类的加载 3.创建类的对象几种方式 二、获取运行时类的完整结构 三、调用运行时类的指定结构 一、java反射机制概述 Reflection (反射)被视为动态语言的关键,为什么这么说呢&am…...
济南智能网站建设咨询电话/搜索引擎营销方案例子
文章目录Transformation算子Actions算子说明:spark 算子分为两类:一类是Transformation算子,一类是Action算子,其中Transformation算子不会触发作业提交,Action算子会触发作业提交。Transformation算子 map࿰…...
中山外贸出口网站建设多少钱哪里有/网站建设的流程是什么
问题:DirectX诊断工具中,“显示”选项卡“DirectX功能”中DirectDraw、Direct3D还有AGP纹理加速全部都是“不可用”。原因分析:默认没有开启显示加速。(还有很多多媒体方面的功能,XP是默认开启,但是2003没有…...