Json learning [detailed]

What is JSON?

JSON (JavaScript Object Notation, JS Object Notation) is a lightweight data exchange format that is currently widely used.

  • Store and represent data in a text format that is completely independent of the programming language.

  • The simplicity and clear hierarchy make JSON an ideal data interchange language.

  • It is easy for people to read and write, and it is also easy for machines to parse and generate, and it can effectively improve the efficiency of network transmission.

In the JavaScript language, everything is an object. Therefore, any type supported by JavaScript can be represented by JSON, such as strings, numbers, objects, arrays, and so on. Take a look at his requirements and syntax format:

  1. Objects are represented as key-value pairs, with data separated by commas

  2. Curly braces hold objects

  3. Square brackets hold arrays

Convert between 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"}');
//结果是 {a: 'Hello', b: 'World'}

To convert a JavaScript object to a JSON string, use the JSON.stringify() method:

var json = JSON.stringify({
    
    a: 'Hello', b: 'World'});
//结果是 '{"a": "Hello", "b": "World"}'

Controller returns JSON data

Jackson should be a relatively good 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, and we need to import its jar package to use it;

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.8</version>
</dependency>

Here we need two new things, one is @ResponseBody, 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 {
    
    
       //创建一个jackson的对象映射器,用来解析数据
       ObjectMapper mapper = new ObjectMapper();
       //创建一个对象
       User user = new User("张三", 3, "男");
       //将我们的对象解析成为json格式
       String str = mapper.writeValueAsString(user);
       //由于@ResponseBody注解,这里会将str转成json格式返回;十分方便
       return str;
  }
}

Uniform solution to garbled characters

1.//produces: Specify the response body return type and encoding
@RequestMapping(value = "/json1", produces = "application/json;charset=utf-8")

2. We can add a message StringHttpMessageConverter conversion configuration to the configuration file of springmvc!

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

For more details, please refer to https://mp.weixin.qq.com/s/RAqRKZJqsJ78HRrJg71R1g
.

Guess you like

Origin blog.csdn.net/cst522445906/article/details/120211638