Question: How to parse JSON data and what are the methods?

This question is an operation that we, as programmers, do almost every day. How many ways does it have? Let's talk about this here.

Four ways of analysis

  • Official analysis

  • Google Gson analysis

  • Alibaba FastJson analysis

  • Jackson analysis

Case practice

Here we only discuss how to convert objects and json.

Here we first create a Maven project to facilitate the introduction of dependencies, and actually test our different resolution methods through unit testing.

Below we all use the User object, first create the classes we need.

User.java

package org.example;

public class User {

private String name;
private String sex;
private Integer age;

public User() {
}

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

public String getName() {
return name;
}

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

public String getSex() {
return sex;
}

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

public Integer getAge() {
return age;
}

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

@Override
public String toString() {
return “User{” +
“name=’” + name + ‘’’ +
“, sex=’” + sex + ‘’’ +
“, age=” + age +
‘}’;
}

}

Official analysis

The official json parsing method is more cumbersome, so fewer people use it.

The first step is to introduce dependencies:

org.json json 20160810

The second step is to write test code:

package org.example;

import org.json.JSONObject;
import org.springframework.stereotype.Component;

/**

  • Official parsing
    /
    @Component
    public class JsonTest1 { /

    *
  • Object to json
  • @param user
  • @return
    /
    public String userToJson(User user){
    JSONObject jsonObject = new JSONObject(user);
    String jsonStr = jsonObject.toString();
    return jsonStr;
    }

    /
    *
  • json to object
  • @param json
  • @return
    */
    public User userFromJson(String json){
    JSONObject jsonObject = new JSONObject(json);
    String name = jsonObject.getString(“name”);
    String sex = jsonObject.getString(“sex”);
    int age = jsonObject.getInt(“age”);
    User user = new User(name,sex,age);
    return user;
    }

    }

The third step, unit testing:

package org.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = “classpath:spring.xml”)
public class JsonTest1Test {

@Autowired
private JsonTest1 jsonTest1;

@Test
public void userToJson() {
User user = new User(“秦明”, “man”, 18);
String json = jsonTest1.userToJson(user);
System.out.println(json);
}

@Test
public void userFromJson() {
String json = “{“sex”:“man”,“name”:“秦明”,“age”:18}”;
User user = jsonTest1.userFromJson(json);
System.out.println(user);
}
}

The fourth step is to view the results:

Google Gson analysis

The first step is to introduce dependencies:

com.google.code.gson gson 2.8.5

The second step is to write test code:

package org.example;

import com.google.gson.Gson;
import org.springframework.stereotype.Component;

/**

  • Analytical gson
    /
    @Component
    public class JsonTest2 { /

    *
  • Object to json
  • @param user
  • @return
    /
    public String userToJson(User user){
    Gson gson = new Gson();
    String json = gson.toJson(user);
    return json;
    }

    /
    *
  • json to object
  • @param json
  • @return
    */
    public User userFromJson(String json){
    Gson gson = new Gson();
    User user = gson.fromJson(json, User.class);
    return user;
    }

    }

The third step, unit test: Same as above (the third and fourth steps below are the same as above, so the following is omitted)

The fourth step, check the result: Same as above

Alibaba FastJson analysis

This analytical method was developed by Alibaba. It is the most efficient, well-loved, and powerful. Those who want to learn more can check the FastJson official website API.

The first step is to introduce dependencies:

com.alibaba fastjson 1.2.47

The second step is to write test code:

package org.example;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

/**

  • fastjson解析
    /
    @Component
    public class JsonTest3 {

    /
    *
  • Object to json
  • @param user
  • @return
    /
    public String userToJson(User user){
    String json = JSONObject.toJSONString(user);
    return json;
    }

    /
    *
  • json to object
  • @param json
  • @return
    */
    public User userFromJson(String json){
    User user = JSONObject.parseObject(json,User.class);
    return user;
    }

    }

Jackson analysis

This parsing method is currently using some of the most popular frameworks such as SSM and SpringBoot. Jackson is used for internal json parsing, but we often introduce FastJson to use it because it is faster.

The first step is to introduce dependencies:

com.fasterxml.jackson.core jackson-databind 2.9.8

The second step is to write test code:

package org.example;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**

  • jackson解析
    /
    @Component
    public class JsonTest4 {

    /
    *
  • Object to json
  • @param user
  • @return
    /
    public String userToJson(User user) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(user);
    return json;
    }

    /
    *
  • json to object
  • @param json
  • @return
    */
    public User userFromJson(String json) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    User user = objectMapper.readValue(json, User.class);
    return user;
    }

    }

Extension ~ the birth of JSON

JSON was born because the implementation details of the integration of XML into HTML in each browser are not the same, so Douglas Crockford and Chip Morningstar together from JS data types A subset was extracted as a new data exchange format. Because mainstream browsers use common JavaScript engine components, there is no compatibility problem when parsing this new data format, so they named this data format "JavaScript Object Notation", abbreviated as JSON, and thus JSON was born!

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/109214581