SoFunction
Updated on 2025-04-11

Detailed explanation of Json data interaction processing examples in SpringMVC

Json Overview

Json Constituents

  • Basic data types:JSON defines a few basic data types, namely numbers, booleans, strings, arrays, objects, and null. These data types form the basis for JSON to process data, and they are compatible with native data types in most programming languages.
  • Object representation: In JSON, objects are sets of key-value pairs, enclosed in curly braces. Each key name is followed by a colon and the corresponding value, separated by commas between different key-value pairs. The keys of an object are usually represented by strings surrounded by double quotes.
  • Array representation: Arrays are ordered sets of values ​​enclosed in square brackets. The values ​​in the array can be of a simple type, or an object or other array, and the arrays can be nested.
  • Diversity of values: The value of JSON can be of a simple type, or an object or an array. This flexible structure allows JSON to efficiently represent complex data structures.
  • Escape of strings: JSON strings must be enclosed in double quotes and can contain escape characters to represent characters that cannot be entered directly, such as carriage return or tab characters.

The relationship between JSON and JavaScript

  • Serialization and deserialization: In JavaScript, you can use()Methods serialize an object into a string in JSON format, and()The method can deserialize a JSON-formatted string back to a JavaScript object.
  • Language irrelevant: Although JSON is based on JavaScript-based syntax, it is language-independent. Many other programming languages ​​also provide libraries for parsing and generating JSON data, which reflects the universality of JSON as a data exchange format.

Advantages of JSON

  • Strong readability: Because JSON adopts a concise text format, it is very easy to be read and understood by people, and it is also easy to parse by programs.
  • Fast parsing speed: Compared to XML, JSON has a smaller size and faster parsing speed. Its original design is to improve the efficiency of data exchange.
  • Cross-language support: The widely supported features of JSON make it an ideal data exchange format between different programming environments. Whether it is a dynamic or a static language, the corresponding JSON parsing library can be found.

JSON application

  • Web Applications: In web development, JSON is often used for data transmission between clients and servers. Due to its lightweight nature, it is very suitable for AJAX communications.
  • API Services: Modern Web APIs, such as RESTful API, usually choose JSON as its primary data exchange format because it is simple, flexible and easy to scale.
  • Configuration File: The JSON format is also used as the configuration file of the application, which can store application settings and parameters, making it easier for program reading and modification

Use of JSON tools

  • Format and verification: The online JSON tool allows users to format and verify JSON data, helping developers quickly locate format errors and correct them.
  • Data conversion: Some tools also provide the conversion of JSON to specific data structures of various programming languages, such as converting JSON to Java or C# entity classes.
  • Encoding and decoding: For special encoding requirements, such as URL encoding or Base64 encoding, there are also corresponding tools that support the encoding and decoding operations of JSON data.

returnUnified solution to json strings

Use @RestController  directly on the class, so that all the methods inside will only return a json string.

No need to add @ResponseBody for each one! We usually use @RestController in the front and back end separate delivery, which is very convenient!

For example:

@RestController
public class UserController {
//produces: Specify the response body return type and encoding@RequestMapping(value = "/json1")
public String json1() throws JsonProcessingException {
//Create a jackson object mapper to parse dataObjectMapper mapper = new ObjectMapper();
//Create an objectUser user = new User("Qinjiang No. 1", 3, "male");
//Parse our object into json formatString str = (user);
// Due to @ResponseBody annotation, str will be converted to json format and returned here; it is very convenientreturn str;
}
}

FastJson

It is a package developed by Alibaba that is specially used for Java development, which can easily implement json objects and JavaBeans.Object conversion, implements the conversion of JavaBean objects and json strings, and implements the conversion of 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></groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>

fastjson three main classes:

JSONObject representjson Object

  • JSONObject implements the Map interface, guess that the underlying operation of JSONObject is implemented by Map.
  • JSONObject corresponds to json objects. The data in the json object can be obtained through various forms of get() methods, and the number of "key:value" pairs can be obtained by using methods such as size() and isEmpty() and determine whether it is empty. Its essence is accomplished by implementing the Map interface and calling methods in the interface.

JSONArray representjson Object array

  • There are methods in the List interface to complete the operation.

JSON representJSONObject and JSONArray Conversion

  • JSON class source code analysis and use
  • If you observe these methods carefully, it mainly implements the mutual conversion between json objects, json object arrays, javabean objects, and json strings.

Summarize

This is the article about Json data interaction processing in SpringMVC. For more related SpringMVC Json data interaction processing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!