Json字符串转换为Java对象和应用(使用fastjson工具类)

记录:468

场景:把Json字符串转换为Java对象。Java对象属性包括String、List、Map等类型。

版本:JDK 1.8,Spring Boot 2.6.3,fastjson-2.0.33。

JSON:JavaScript Object Notation(JavaScript 对象表示法)。

JSON语法规则:数据由名称/值对组成。数据由逗号","分隔。使用斜杆\来转义字符。大括号{}保存对象。中括号[]保存数组(或者List类型),数组可以包含多个对象。

1.JSON字符串转换为JSONObject

1.1JSON字符串

{
  "cityDescribe": "宁波是港口城市",
  "cityId": 2023062802,
  "cityName": "宁波",
  "updateTime": "2023-07-16 13:15:40"
}

1.2转换代码

public static void f1() {
  String jsonStr = "{\n" +
          "  \"cityDescribe\": \"宁波是港口城市\",\n" +
          "  \"cityId\": 2023062802,\n" +
          "  \"cityName\": \"宁波\",\n" +
          "  \"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "}";
  System.out.println("====================f1====================");
  JSONObject obj = JSON.parseObject(jsonStr);
  System.out.println(obj.get("cityId"));
  System.out.println(obj.get("cityName"));
  System.out.println(obj.get("cityDescribe"));
  System.out.println(obj.get("updateTime"));
}

2.JSON字符串转换为CityDto

2.1JSON字符串

{
  "cityDescribe": "宁波是港口城市",
  "cityId": 2023062802,
  "cityName": "宁波",
  "updateTime": "2023-07-16 13:15:40"
}

2.2转换代码

public static void f2() {
  String jsonStr = "{\n" +
          "  \"cityDescribe\": \"宁波是港口城市\",\n" +
          "  \"cityId\": 2023062802,\n" +
          "  \"cityName\": \"宁波\",\n" +
          "  \"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "}";
  System.out.println("====================f2====================");
  CityDto cityDto = JSON.parseObject(jsonStr, CityDto.class);
  System.out.println(cityDto.getCityId());
  System.out.println(cityDto.getCityName());
  System.out.println(cityDto.getCityDescribe());
  System.out.println(cityDto.getUpdateTime());
}

2.3实体对象

@Data
@Builder
public class CityDto implements Serializable {
    private Long cityId;
    private String cityName;
    private String cityDescribe;
    private String updateTime;
}

3.JSON字符串转换为CityObjectDto,对象属性中是一个对象

3.1JSON字符串

{
	"cityObj": {
		"cityName": "宁波",
		"updateTime": "2023-07-16 13:15:40",
		"cityId": 2023062802,
		"cityDescribe": "宁波是港口城市"
	}
}

3.2转换代码

public static void f3() {
  String jsonStr = "{\n" +
          "  \"cityObj\": {\n" +
          "  \t\"cityName\": \"宁波\",\n" +
          "  \t\"updateTime\": \"2023-07-16 13:15:40\",\n" +
          "  \t\"cityId\": 2023062802,\n" +
          "  \t\"cityDescribe\": \"宁波是港口城市\"\n" +
          "  }\n" +
          "}";
  System.out.println("====================f3====================");
  CityObjectDto cityDto = JSON.parseObject(jsonStr, CityObjectDto.class);
  System.out.println(cityDto.getCityObj().getCityId());
  System.out.println(cityDto.getCityObj().getCityName());
  System.out.println(cityDto.getCityObj().getCityDescribe());
  System.out.println(cityDto.getCityObj().getCityDescribe());
}

3.3实体对象

@Data
@Builder
public class CityObjectDto implements Serializable {
    private CityDto cityObj;
}

4.JSON字符串转换为Map

4.1JSON字符串

{
  "cityDescribe": "宁波是港口城市",
  "cityId": 2023062802,
  "cityName": "宁波",
  "updateTime": "2023-07-16 13:15:40"
}

4.2转换代码

public static void f4() {
  String jsonStr = "{\n" +
          "  \"cityDescribe\": \"宁波是港口城市\",\n" +
          "  \"cityId\": 2023062802,\n" +
          "  \"cityName\": \"宁波\",\n" +
          "  \"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "}";
  System.out.println("====================f4====================");
  Map mapObj = JSON.parseObject(jsonStr, Map.class);
  System.out.println(mapObj.get("cityId"));
  System.out.println(mapObj.get("cityName"));
  System.out.println(mapObj.get("cityDescribe"));
  System.out.println(mapObj.get("updateTime"));
}

5.JSON字符串转换为CityMapDto,对象属性是一个Map类型

5.1JSON字符串

{
  "cityMap": {
  	"cityName": "宁波",
  	"updateTime": "2023-07-16 13:15:40",
  	"cityId": 2023062802,
  	"cityDescribe": "宁波是港口城市"
  }
}

5.2转换代码

public static void f5() {
  String jsonStr = "{\n" +
          "  \"cityMap\": {\n" +
          "  \t\"cityName\": \"宁波\",\n" +
          "  \t\"updateTime\": \"2023-07-16 13:15:40\",\n" +
          "  \t\"cityId\": 2023062802,\n" +
          "  \t\"cityDescribe\": \"宁波是港口城市\"\n" +
          "  }\n" +
          "}";
  System.out.println("====================f5====================");
  CityMapDto cityMapDto = JSON.parseObject(jsonStr, CityMapDto.class);
  System.out.println(cityMapDto.getCityMap().get("cityId"));
  System.out.println(cityMapDto.getCityMap().get("cityName"));
  System.out.println(cityMapDto.getCityMap().get("cityDescribe"));
  System.out.println(cityMapDto.getCityMap().get("updateTime"));
}

5.3实体对象

@Data
@Builder
public class CityMapDto implements Serializable {
    private Map cityMap;
}

6.JSON字符串转换为List

6.1JSON字符串

[{
	"cityDescribe": "杭州是互联网城市",
	"cityId": 2023062801,
	"cityName": "杭州",
	"updateTime": "2023-07-16 13:15:40"
}, {
	"cityDescribe": "宁波是港口城市",
	"cityId": 2023062802,
	"cityName": "宁波",
	"updateTime": "2023-07-16 13:15:40"
}]

6.2转换代码

public static void f6() {
  String jsonStr = "[{\n" +
          "\t\"cityDescribe\": \"杭州是互联网城市\",\n" +
          "\t\"cityId\": 2023062801,\n" +
          "\t\"cityName\": \"杭州\",\n" +
          "\t\"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "}, {\n" +
          "\t\"cityDescribe\": \"宁波是港口城市\",\n" +
          "\t\"cityId\": 2023062802,\n" +
          "\t\"cityName\": \"宁波\",\n" +
          "\t\"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "}]";
  System.out.println("====================f6====================");
  List cityList = JSON.parseArray(jsonStr, CityDto.class);
  cityList.forEach((cityDto) -> {
              System.out.println(cityDto.getCityId());
              System.out.println(cityDto.getCityName());
              System.out.println(cityDto.getCityDescribe());
              System.out.println(cityDto.getUpdateTime());
          }
  );
}

7.JSON字符串转换为CityListDto,对象属性是一个List类型

7.1JSON字符串

{
	"cityList": [{
		"cityDescribe": "杭州是互联网城市",
		"cityId": 2023062801,
		"cityName": "杭州",
		"updateTime": "2023-07-16 13:15:40"
	}, {
		"cityDescribe": "宁波是港口城市",
		"cityId": 2023062802,
		"cityName": "宁波",
		"updateTime": "2023-07-16 13:15:40"
	}]
}

7.2转换代码

public static void f7() {
  String jsonStr = "{\n" +
          "\t\"cityList\": [{\n" +
          "\t\t\"cityDescribe\": \"杭州是互联网城市\",\n" +
          "\t\t\"cityId\": 2023062801,\n" +
          "\t\t\"cityName\": \"杭州\",\n" +
          "\t\t\"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "\t}, {\n" +
          "\t\t\"cityDescribe\": \"宁波是港口城市\",\n" +
          "\t\t\"cityId\": 2023062802,\n" +
          "\t\t\"cityName\": \"宁波\",\n" +
          "\t\t\"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "\t}]\n" +
          "}";
  System.out.println("====================f7====================");
  CityListDto cityListDto = JSON.parseObject(jsonStr, CityListDto.class);
  List cityList = JSON.parseArray(cityListDto.getCityList().toString(), CityDto.class);
  cityList.forEach((cityDto) -> {
              System.out.println(cityDto.getCityId());
              System.out.println(cityDto.getCityName());
              System.out.println(cityDto.getCityDescribe());
              System.out.println(cityDto.getUpdateTime());
          }
  );
}

7.3实体对象

@Data
@Builder
public class CityListDto implements Serializable {
    private List cityList;
}

8.复杂的JSON字符串转换为ProvinceDto,在对象中包含对象和List类型

8.1JSON字符串

{
	"provinceName": "浙江省",
	"areaNumber": "330000",
	"data": {
		"dataFlag": "01",
		"cityData": {
			"dataList": [{
				"cityDescribe": "杭州是互联网城市",
				"cityId": 2023062801,
				"cityName": "杭州",
				"updateTime": "2023-07-16 13:15:40"
			}, {
				"cityDescribe": "宁波是港口城市",
				"cityId": 2023062802,
				"cityName": "宁波",
				"updateTime": "2023-07-16 13:15:40"
			}]
		}
	}
}

8.2转换代码

public static void f8() {
  String jsonStr = "{\n" +
          "\t\"provinceName\": \"浙江省\",\n" +
          "\t\"areaNumber\": \"330000\",\n" +
          "\t\"data\": {\n" +
          "\t\t\"dataFlag\": \"01\",\n" +
          "\t\t\"cityData\": {\n" +
          "\t\t\t\"dataList\": [{\n" +
          "\t\t\t\t\"cityDescribe\": \"杭州是互联网城市\",\n" +
          "\t\t\t\t\"cityId\": 2023062801,\n" +
          "\t\t\t\t\"cityName\": \"杭州\",\n" +
          "\t\t\t\t\"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "\t\t\t}, {\n" +
          "\t\t\t\t\"cityDescribe\": \"宁波是港口城市\",\n" +
          "\t\t\t\t\"cityId\": 2023062802,\n" +
          "\t\t\t\t\"cityName\": \"宁波\",\n" +
          "\t\t\t\t\"updateTime\": \"2023-07-16 13:15:40\"\n" +
          "\t\t\t}]\n" +
          "\t\t}\n" +
          "\t}\n" +
          "}";
  System.out.println("====================f8====================");
  //1.解析JSON字符串(嵌套JSON对象逐层解析)
  ProvinceDto provinceDto = JSON.parseObject(jsonStr, ProvinceDto.class);
  DataDto data = JSON.parseObject(provinceDto.getData().toString(), DataDto.class);
  CityDataDto cityData = JSON.parseObject(data.getCityData().toString(), CityDataDto.class);
  List dataList = JSON.parseArray(cityData.getDataList().toString(), CityDto.class);
  //2.解析完成后转换为Java对象
  CityDataDto cityDataResult = CityDataDto.builder()
          .dataList(dataList)
          .build();
  DataDto dataResult = DataDto.builder()
          .dataFlag(data.getDataFlag())
          .cityData(cityDataResult)
          .build();
  ProvinceDto provinceDtoResult = ProvinceDto.builder()
          .provinceName(provinceDto.getProvinceName())
          .areaNumber(provinceDto.getAreaNumber())
          .data(dataResult)
          .build();
  //3.使用Java对象
  System.out.println(provinceDtoResult.getProvinceName());
  System.out.println(provinceDtoResult.getAreaNumber());
  DataDto dataDtoApp = (DataDto) provinceDtoResult.getData();
  System.out.println(dataDtoApp.getDataFlag());
  CityDataDto cityDataDtoApp = (CityDataDto) dataDtoApp.getCityData();
  List dataListApp = (List) cityDataDtoApp.getDataList();
  dataListApp.forEach((cityDto) -> {
              System.out.println(cityDto.getCityId());
              System.out.println(cityDto.getCityName());
              System.out.println(cityDto.getCityDescribe());
              System.out.println(cityDto.getUpdateTime());
          }
  );
}

8.3实体对象

8.3.1Java对象ProvinceDto

(1)示例代码

@Data
@Builder
public class ProvinceDto implements Serializable {
  private String provinceName;
  private String areaNumber;
  private T data;
}

(2)解析代码

ProvinceDto对象对应Json字符串的provinceName、areaNumber、data属性。

8.3.2Java对象DataDto

(1)示例代码

@Data
@Builder
public class DataDto implements Serializable {
  private String dataFlag;
  private T cityData;
}

(2)解析代码

DataDto对象对应JSON字符串的data属性的dataFlag、cityData属性。

8.3.3Java对象CityDataDto

(1)示例代码

@Data
@Builder
public class CityDataDto implements Serializable {
  private T dataList;
}

(2)解析代码

CityDataDto对象对应JSON字符串的data属性的cityData属性的dataList属性。

8.3.4Java对象CityDto

(1)示例代码

@Data
@Builder
public class CityDto implements Serializable {
  private Long cityId;
  private String cityName;
  private String cityDescribe;
  private String updateTime;
}

2)解析代码

CityDto 对象对应JSON字符串的data属性的cityData属性的dataList属性的List中的对象。

以上,感谢。

2023年7月16日

你可能感兴趣的:(L02-Java基础,java,Json字符串转换Java对象,Json字符串转换,fastjson,JSON)