Convert Json strings to Java objects and applications (using the fastjson tool class)

Records : 468

Scenario : Convert Json strings to Java objects. Java object properties include types such as String, List, and Map.

Versions : JDK 1.8, Spring Boot 2.6.3, fastjson-2.0.33.

JSON : JavaScript Object Notation (JavaScript Object Notation).

JSON syntax rules : Data consists of name/value pairs. Data is separated by a comma ",". Use a slash \ to escape characters. Braces { } hold objects. Brackets [] save the array (or List type), and the array can contain multiple objects.

1. Convert JSON string to JSONObject

1.1 JSON string

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

1.2 Conversion code

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. Convert JSON string to CityDto

2.1 JSON string

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

2.2 Conversion code

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 entity object

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

3. Convert the JSON string to CityObjectDto, and the object property is an object

3.1 JSON string

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

3.2 Conversion code

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 Entity objects

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

4. Convert JSON string to Map

4.1 JSON string

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

4.2 Conversion code

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. The JSON string is converted to CityMapDto, and the object property is a Map type

5.1 JSON string

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

5.2 Conversion code

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 Entity objects

@Data
@Builder
public class CityMapDto implements Serializable {
    private Map<String, Object> cityMap;
}

6. Convert JSON string to List

6.1 JSON string

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

6.2 Conversion code

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<CityDto> 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. Convert the JSON string to CityListDto, and the object property is a List type

7.1 JSON string

{
	"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 Conversion code

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<CityDto> 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 Entity objects

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

8. Convert complex JSON strings to ProvinceDto, including objects and List types in objects

8.1 JSON string

{
	"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 Conversion code

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<CityDto> 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<CityDto> dataListApp = (List<CityDto>) 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 Entity objects

8.3.1 Java object ProvinceDto

(1) sample code

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

(2) Analysis code

The ProvinceDto object corresponds to the provinceName, areaNumber, and data attributes of the Json string.

8.3.2 Java object DataDto

(1) sample code

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

(2) Analysis code

The DataDto object corresponds to the dataFlag and cityData attributes of the data attribute of the JSON string.

8.3.3 Java object CityDataDto

(1) sample code

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

(2) Analysis code

The CityDataDto object corresponds to the dataList property of the cityData property of the data property of the JSON string.

8.3.4 Java object CityDto

(1) sample code

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

2) Parse the code

The CityDto object corresponds to the objects in the List of the dataList attribute of the cityData attribute of the data attribute of the JSON string.

Above, thanks.

July 16, 2023

Guess you like

Origin blog.csdn.net/zhangbeizhen18/article/details/131751182