@Test
public void JSONStringTOJSONObject(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
// 获取简单对象
String teacherName = jsonObject.getString(“teacherName”);
Integer teacherAge = jsonObject.getInteger(“teacherAge”);
System.out.println("teacherName: " + teacherName + ",teacherAge " + teacherAge);
// 获取JSONObject对象
JSONObject course = jsonObject.getJSONObject(“course”);
// 获取JSONObject中的数据
String courseName = course.getString(“courseName”);
Integer code = course.getInteger(“code”);
System.out.println("courseName: " + courseName + " code: " + code);
// 获取JSONArray对象
JSONArray students = jsonObject.getJSONArray(“students”);
// 获取JSONArray的中的数据
Iterator iterator = students.iterator();
while (iterator.hasNext()){
JSONObject jsonObject1 = (JSONObject) iterator.next();
System.out.println("studentName: " + jsonObject1.getString(“studentName”) + ",StudentAge: "
- jsonObject1.getInteger(“studentAge”));
}
}
用JSON.toJSONString()方法即可将复杂JSONObject转化为JSON字符串
/**
- 复杂JSONObject到json字符串的转换
*/
@Test
public void JSONObjectTOJSON(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
String s = JSON.toJSONString(jsonObject);
System.out.println(s);
}
定义JavaBean类
package com.fastjson;
public class Student {
private String studentName;
private int studentAge;
public Student() {
}
public Student(String studentName, int studentAge) {
this.studentName = studentName;
this.studentAge = studentAge;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}
@Override
public String toString() {
return “Student{” +
“studentName='” + studentName + ‘’’ +
“, studentAge=” + studentAge +
‘}’;
}
}
package com.jiyong.config;
/**
-
对于复杂嵌套的JSON格式,利用JavaBean进行转换的时候要注意
-
1、有几个JSONObject就定义几个JavaBean
-
2、内层的JSONObject对应的JavaBean作为外层JSONObject对应的JavaBean的一个属性
-
3、解析方法有两种
-
第一种方式,使用TypeReference类
-
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference() {})
-
第二种方式,使用Gson思想
-
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class);
*/
import java.util.List;
public class Teacher {
private String teacherName;
private int teacherAge;
private Course course;
private List students;
public Teacher() {
}
public Teacher(String teacherName, int teacherAge, Course course, List students) {
this.teacherName = teacherName;
this.teacherAge = teacherAge;
this.course = course;
this.students = students;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public int getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(int teacherAge) {
this.teacherAge = teacherAge;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
@Override
public String toString() {
return “Teacher{” +
“teacherName='” + teacherName + ‘’’ +
“, teacherAge=” + teacherAge +
“, course=” + course +
“, students=” + students +
‘}’;
}
}
package com.jiyong.config;
public class Course {
private String courseName;
private int code;
public Course() {
}
public Course(String courseName, int code) {
this.courseName = courseName;
this.code = code;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@Override
public String toString() {
return “Course{” +
“courseName='” + courseName + ‘’’ +
“, code=” + code +
‘}’;
}
}
Jason字符串转换为JavaBean有三种方式,推荐通过反射的方式。
/**
-
json字符串-简单对象到JavaBean之间的转换
-
1、定义JavaBean对象
-
2、Jason字符串转换为JavaBean有三种方式,推荐通过反射的方式
*/
@Test
public void JSONStringToJavaBeanObj(){
// 第一种方式
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
String studentName = jsonObject.getString(“studentName”);
Integer studentAge = jsonObject.getInteger(“studentAge”);
Student student = new Student(studentName, studentAge);
// 第二种方式,//第二种方式,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类
Student student1 = JSON.parseObject(JSON_OBJ_STR, new TypeReference() {});
// 第三种方式,通过反射,建议这种方式
Student student2 = JSON.parseObject(JSON_OBJ_STR, Student.class);
}
也是通过JSON的toJSONString,不管是JSONObject、JSONArray还是JavaBean转为为JSON字符串都是通过JSON的toJSONString方法。
/**
- JavaBean转换为Json字符串,也是通过JSON的toJSONString,不管是JSONObject、JSONArray还是JavaBean转为为JSON字符串都是通过JSON的toJSONString方法
*/
@Test
public void JavaBeanToJsonString(){
Student lily = new Student(“lily”, 12);
String s = JSON.toJSONString(lily);
System.out.println(s);
}
/**
- json字符串-数组类型到JavaBean_List的转换
*/
@Test
public void JSONStrToJavaBeanList(){
// 方式一:
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
//遍历JSONArray
List students = new ArrayList();
Iterator iterator = jsonArray.iterator();
while (iterator.hasNext()){
JSONObject next = (JSONObject) iterator.next();
String studentName = next.getString(“studentName”);
Integer studentAge = next.getInteger(“studentAge”);
Student student = new Student(studentName, studentAge);
students.add(student);
}
// 方式二,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类
List studentList = JSON.parseObject(JSON_ARRAY_STR,new TypeReference<ArrayList>() {});
// 方式三,使用反射
List students1 = JSON.parseArray(JSON_ARRAY_STR, Student.class);
System.out.println(students1);
}
3.10 JavaBean-List —》json字符串数组
/**
- JavaBean_List到json字符串-数组类型的转换,直接调用JSON.toJSONString()方法即可
*/
@Test
public void JavaBeanListToJSONStr(){
Student student = new Student(“lily”, 12);
Student student1 = new Student(“lucy”, 13);
List students = new ArrayList();
students.add(student);
students.add(student1);
String s = JSON.toJSONString(student);
System.out.println(s);
}
3.11 复杂嵌套json格式字符串—》JavaBean_obj
对于复杂嵌套的JSON格式,利用JavaBean进行转换的时候要注意:
-
有几个JSONObject就定义几个JavaBean内层的JSONObject
-
对应的JavaBean作为外层JSONObject对应的JavaBean的一个属性
/**
- 复杂json格式字符串到JavaBean_obj的转换
*/
@Test
public void ComplexJsonStrToJavaBean(){
//第一种方式,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类
Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference() {});
先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以扫码领取!

Spring全套教学资料
Spring是Java程序员的《葵花宝典》,其中提供的各种大招,能简化我们的开发,大大提升开发效率!目前99%的公司使用了Spring,大家可以去各大招聘网站看一下,Spring算是必备技能,所以一定要掌握。
目录:
部分内容:
Spring源码
- 第一部分 Spring 概述
- 第二部分 核心思想
- 第三部分 手写实现 IoC 和 AOP(自定义Spring框架)
- 第四部分 Spring IOC 高级应用
基础特性
高级特性 - 第五部分 Spring IOC源码深度剖析
设计优雅
设计模式
注意:原则、方法和技巧 - 第六部分 Spring AOP 应用
声明事务控制 - 第七部分 Spring AOP源码深度剖析
必要的笔记、必要的图、通俗易懂的语言化解知识难点
脚手框架:SpringBoot技术
它的目标是简化Spring应用和服务的创建、开发与部署,简化了配置文件,使用嵌入式web服务器,含有诸多开箱即用的微服务功能,可以和spring cloud联合部署。
Spring Boot的核心思想是约定大于配置,应用只需要很少的配置即可,简化了应用开发模式。
- SpringBoot入门
- 配置文件
- 日志
- Web开发
- Docker
- SpringBoot与数据访问
- 启动配置原理
- 自定义starter
微服务架构:Spring Cloud Alibaba
同 Spring Cloud 一样,Spring Cloud Alibaba 也是一套微服务解决方案,包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
- 微服务架构介绍
- Spring Cloud Alibaba介绍
- 微服务环境搭建
- 服务治理
- 服务容错
- 服务网关
- 链路追踪
- ZipKin集成及数据持久化
- 消息驱动
- 短信服务
- Nacos Confifig—服务配置
- Seata—分布式事务
- Dubbo—rpc通信
Spring MVC
目录:
部分内容:
[外链图片转存中…(img-WQFhlZvd-1711392991007)]
[外链图片转存中…(img-LU1tM2VD-1711392991007)]
微服务架构:Spring Cloud Alibaba
同 Spring Cloud 一样,Spring Cloud Alibaba 也是一套微服务解决方案,包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
- 微服务架构介绍
- Spring Cloud Alibaba介绍
- 微服务环境搭建
- 服务治理
- 服务容错
- 服务网关
- 链路追踪
- ZipKin集成及数据持久化
- 消息驱动
- 短信服务
- Nacos Confifig—服务配置
- Seata—分布式事务
- Dubbo—rpc通信
[外链图片转存中…(img-1YksTyOf-1711392991008)]
[外链图片转存中…(img-TOfcel48-1711392991008)]
Spring MVC
目录:
[外链图片转存中…(img-kWBECs8I-1711392991008)]
[外链图片转存中…(img-oYNF8lrX-1711392991009)]
[外链图片转存中…(img-rAKRfoG3-1711392991009)]
部分内容:
[外链图片转存中…(img-pEhOMGif-1711392991009)]
[外链图片转存中…(img-Gd8I4Dqk-1711392991009)]
需要更多Java资料的小伙伴可以帮忙点赞+关注,点击传送门,即可免费领取!