5. Jason interactive processing
JSON
5.1 what is JSON?
- JSON (JavaScript object notation) is a lightweight data exchange format, which is widely used at present.
- Data is stored and represented in a text format completely independent of the programming language.
- The concise and clear hierarchy makes JSON an ideal data exchange language.
- It is easy for people to read and write, but also easy for machine analysis and generation, and effectively improves the network transmission efficiency.
In the JavaScript language, everything is an object. Therefore, any type supported by JavaScript can be represented by JSON, such as string, number, object, array, etc. Look at his requirements and syntax format:
- Objects are represented as key value pairs, and data is separated by commas
- Curly braces save objects
- Square brackets hold the array
JSON key value pairs are a way to save JavaScript objects, and the writing method is similar to that of JavaScript objects. The key name in the key / value pair combination is written in front and wrapped in double quotation marks "", separated by colon: and then followed by the value:
{"name": "QinJiang"} {"age": "3"} {"sex": "male"}
Many people don't know the relationship between JSON and JavaScript objects, even who is who. In fact, it can be understood as follows:
JSON is a string representation of JavaScript objects. It uses text to represent the information of a JS object, which is essentially a string.
var obj = {a: 'Hello', b: 'World'}; //This is an object. Note that the key name can also be wrapped in quotation marks var json = '{"a": "Hello", "b": "World"}'; //This is a JSON string, which is essentially a string
5.2 conversion of JSON and JavaScript objects
To convert from a JSON string to a JavaScript object, use the JSON.parse() method:
var obj = JSON.parse('{"a": "Hello", "b": "World"}'); //The result is {a: 'Hello', b: 'World'}
To convert from a JavaScript object to a JSON string, use the JSON.stringify() method:
var json = JSON.stringify({a: 'Hello', b: 'World'}); //The result is' {"a": "Hello", "b": "World"} '
5.3 code test
1. Create a new module, spring mvc-05-json, and add web support
2. Create a new json-1.html in the web directory and write the test content
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JSON_Qin Jiang</title> </head> <body> <script type="text/javascript"> //Write a js object var user = { name:"Qin Jiang", age:3, sex:"male" }; //Convert js object to json string var str = JSON.stringify(user); console.log(str); //Convert json string to js object var user2 = JSON.parse(str); console.log(user2.age,user2.name,user2.sex); </script> </body> </html>
3. In IDEA, open it with a browser and view the console output!
5.4Controller returns JSON data
Jackson should be a better json parsing tool at present
Of course, there are more than one tool, such as Alibaba's fastjson and so on.
We use Jackson here. To use Jackson, we need to import its jar package;
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1</version> </dependency>
Configuration required to configure spring MVC
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--1.register servlet--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--Specified by initialization parameters SpringMVC The location of the configuration file is associated--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- Start sequence: the smaller the number, the earlier the start --> <load-on-startup>1</load-on-startup> </servlet> <!--All requests will be rejected springmvc intercept --> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/</url-pattern> </filter-mapping> </web-app>
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Automatically scan the specified package, and submit all the following annotation classes to IOC Container management --> <context:component-scan base-package="com.kuang.controller"/> <!-- view resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- prefix --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- suffix --> <property name="suffix" value=".jsp" /> </bean> </beans>
We randomly write a User entity class, and then we write our test Controller;
package com.kuang.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //lombok needs to be imported @Data @AllArgsConstructor @NoArgsConstructor public class User { private String name; private int age; private String sex; }
Here we need two new things, one is @ ResponseBody and the other is ObjectMapper object. Let's see the specific usage
Write a Controller;
@Controller public class UserController { @RequestMapping("/json1") @ResponseBody public String json1() throws JsonProcessingException { //Create an object mapper for jackson to parse the data ObjectMapper mapper = new ObjectMapper(); //Create an object User user = new User("Qinjiang 1", 3, "male"); //Parse our object into json format String str = mapper.writeValueAsString(user); //Due to the @ ResponseBody annotation, str will be converted to json format and returned here; Very convenient return str; } }
Configure Tomcat and start the test!
http://localhost:8080/json1
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-chzshuhv-163869815568) (E: \ desktop \ document \ JavaWeb \ springmvc. Assets \ 640-1637897624272. Webp)]
It is found that there is a garbled code problem. We need to set its encoding format to utf-8 and its return type;
This is implemented through the @ requestmapping products attribute. Modify the following code
//produces: Specifies the return type and encoding of the response body @RequestMapping(value = "/json1",produces = "application/json;charset=utf-8")
Test again, http://localhost:8080/json1 , garbled code problem OK!
[Note: remember to deal with garbled code when using json]
5.5 code optimization
Unified solution of garbled code
The previous method is troublesome. If there are many requests in the project, each one should be added, which can be specified uniformly through Spring configuration, so you don't have to deal with them every time!
We can add a message StringHttpMessageConverter conversion configuration to the configuration file of spring MVC!
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="failOnEmptyBeans" value="false"/> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
5.5.1 unified solution for returning json strings
Use @ RestController directly on the class. In this way, all the methods in it will only return json strings. There is no need to add @ ResponseBody to each one! We generally use @ RestController in front and back-end separation development, which is very convenient!
@RestController public class UserController { //produces: Specifies the return type and encoding of the response body @RequestMapping(value = "/json1") public String json1() throws JsonProcessingException { //Create an object mapper for jackson to parse the data ObjectMapper mapper = new ObjectMapper(); //Create an object User user = new User("Qinjiang 1", 3, "male"); //Parse our object into json format String str = mapper.writeValueAsString(user); //Due to the @ ResponseBody annotation, str will be converted to json format and returned here; Very convenient return str; } }
Start the tomcat test, and the results are output normally!
5.5.2 test set output
Add a new method
@RequestMapping("/json2") public String json2() throws JsonProcessingException { //Create an object mapper for jackson to parse the data ObjectMapper mapper = new ObjectMapper(); //Create an object User user1 = new User("Qinjiang 1", 3, "male"); User user2 = new User("Qinjiang 2", 3, "male"); User user3 = new User("Qinjiang 3", 3, "male"); User user4 = new User("Qinjiang 4", 3, "male"); List<User> list = new ArrayList<User>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); //Parse our object into json format String str = mapper.writeValueAsString(list); return str; }
Running result: perfect, no problem!
5.5.3 output time object
Add a new method
@RequestMapping("/json3") public String json3() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); //Create a time object, java.util.Date Date date = new Date(); //Parse our object into json format String str = mapper.writeValueAsString(date); return str; }
Operation results:
- The default date format will become a number, which is the number of milliseconds from January 1, 1970 to the current date!
- Jackson will convert the time into timestamps by default
Solution: cancel the timestamps format and customize the time format
@RequestMapping("/json4") public String json4() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); //Without timestamp mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); //Custom date format object SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Specify date format mapper.setDateFormat(sdf); Date date = new Date(); String str = mapper.writeValueAsString(date); return str; }
Running result: the time was successfully output!
5.6 extraction as tools
If you want to use it frequently, it is troublesome. We can encapsulate these codes into a tool class; Let's write it
package com.kuang.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 { 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 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); //Custom date format object SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); //Specify date format mapper.setDateFormat(sdf); try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } }
When we use tool classes, the code is more concise!
@RequestMapping("/json5") public String json5() throws JsonProcessingException { Date date = new Date(); String json = JsonUtils.getJson(date); return json; }
be accomplished! Perfect!
5.7FastJson
fastjson.jar is a package developed by Alibaba for Java development. It can easily realize the conversion between json objects and JavaBean objects, between JavaBean objects and json strings, and between json objects and json strings. There are many conversion methods to implement json, and the final implementation results are the same.
fastjson's pom dependency!
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>
fastjson has three main classes:
JSONObject represents a json object
- JSONObject implements the Map interface. I guess the underlying operation of JSONObject is implemented by Map.
- JSONObject corresponds to the json object. You can obtain the data in the json object through various forms of get() methods. You can also use methods such as size(), isEmpty() to obtain the number of "key: value" pairs and judge whether they are empty. Its essence is accomplished by implementing the Map interface and calling the methods in the interface.
JSONArray represents an array of json objects
- Internally, there are methods in the List interface to complete the operation.
JSON represents the conversion of JSONObject and JSONArray
- Analysis and use of JSON class source code
- Carefully observe these methods, mainly to realize the mutual transformation between json objects, json object arrays, javabean objects and json strings.
For code testing, we create a new FastJsonDemo class
package com.kuang.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.kuang.pojo.User; import java.util.ArrayList; import java.util.List; public class FastJsonDemo { public static void main(String[] args) { //Create an object User user1 = new User("Qinjiang 1", 3, "male"); User user2 = new User("Qinjiang 2", 3, "male"); User user3 = new User("Qinjiang 3", 3, "male"); User user4 = new User("Qinjiang 4", 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 rotation JSON character 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 conversion 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 rotation JSON object ******"); JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2); System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name")); System.out.println("\n****** JSON Object rotation Java object ******"); User to_java_user = JSON.toJavaObject(jsonObject1, User.class); System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user); } }
We only need to master the use of this tool class, and find the corresponding implementation according to the specific business when using it. Like the previous commons IO toolkit, just use it!
Jason is very important in our data transmission. We must learn to use it!
This article comes from Bili Bili Madness theory Study notes