FastJson parsing object or object array usage

1 Background introduction

FastJson parses object or array of objects usage. Fastjson is often used for serialization and deserialization, especially for requests or some temporary parameters. If you don't use it often, you may face various choices and don't know how to use it, so I wrote the following most commonly used usages, hoping to help readers and avoid detours.

You need to import dependencies in advance to use the following interfaces.

			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>fastjson</artifactId>
				<version>1.2.72_noneautotype</version>
			</dependency>

2 JSON to object

Class --------->String--------->Current class
such as the following example:
StudentRequest is converted to StudentDTO

StudentRequest studentRequest = new StudentRequest();
     String s = JSON.toJSONString(studentRequest);
     StudentDTO studentDTO = JSON.parseObject(s, StudentDTO.class);

The attributes in the two are consistent, and information may be lost if they are inconsistent

3 JSON to object array

Class --------->String--------->Current class
such as the following example:
StudentRequest is converted to StudentDTO

List<StudentRequest> studentRequests = new List<StudentRequest>();
     String s = JSON.toJSONString(studentRequests);
     List<StudentDTO> studentDTOs = JSON.parseArray(s, StudentDTO.class);

The attributes in the two are consistent, and information may be lost if they are inconsistent

Guess you like

Origin blog.csdn.net/qq_39463175/article/details/131146484