1. 程式人生 > 其它 >簡單瞭解JSON~~

簡單瞭解JSON~~

技術標籤:json

什麼是JSON?

  • JSON(JavaScript Object Notation,JS物件標記)是一種輕量級的資料交換格式,目前使用特別廣泛。
  • 採用完全獨立於程式語言的文字格式來儲存和表示資料。
  • 簡潔和清晰的層次結構使得JSON成為理想的資料交換語言。
  • 易於人閱讀和編寫,同時也易於機器解析和生成,並有效地提升網路傳輸效率。

程式碼編寫

 <script type="text/javascript">
        //編寫一個js物件
        var user = {
            name: "你好",
age: 3, sex: "男" }; //將js物件轉換為json物件 var json = JSON.stringify(user); console.log(json); console.log(user); //將json物件轉換為js物件 var obj = JSON.parse(json); console.log(obj); </script>

導包

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

遇到亂碼問題,可以在springmvc-servlet.xml裡面配置

<!--    json亂碼問題配置-->
    <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>
package com.lei.pojo;

public class User {
    private String name;
    private int age;
    private String sex;

    public User(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

package com.lei.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lei.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @RequestMapping("/j1")
    @ResponseBody  //就不會走檢視解析器,直接返回一個字串
    public String json1() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User("你好",12,"男");
        String s1 = mapper.writeValueAsString(user);
        return s1;
    }
}

在這裡插入圖片描述

在這裡插入圖片描述
上述輸出了一個物件,試試集合。

package com.lei.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lei.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {

    @RequestMapping("/j1")
    @ResponseBody  //就不會走檢視解析器,直接返回一個字串
    public String json1() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<User> list = new ArrayList<User>();
        User user1 = new User("你好",12,"男");
        User user2 = new User("你好",12,"男");
        User user3 = new User("你好",12,"男");
        User user4 = new User("你好",12,"男");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        String s1 = mapper.writeValueAsString(list);
        return s1;
    }
}

在這裡插入圖片描述

Fastjson

導包

    <dependency>
    <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.60</version>
    </dependency>

package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lei.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {



    @RequestMapping("/j1")
    @ResponseBody  //就不會走檢視解析器,直接返回一個字串
    public String json1() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<User> list = new ArrayList<User>();
        User user1 = new User("你好",12,"男");
        User user2 = new User("你好",12,"男");
        User user3 = new User("你好",12,"男");
        User user4 = new User("你好",12,"男");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        //String s1 = mapper.writeValueAsString(list);
        String s = JSON.toJSONString(list);
        return s;
    }
}

在這裡插入圖片描述
擁有同樣的效果。

他還有以下這些方便的轉換方法

    //Java物件轉json字串
        String s = JSON.toJSONString(user1);
        //json字串轉java物件
        User user = JSON.parseObject(s, User.class);
        //Java物件轉json物件
        JSONObject jsonObject = (JSONObject)JSON.toJSON(user1);
        //json物件轉Java物件
        User user5 = JSON.toJavaObject(jsonObject, User.class);