Home > Java > javaTutorial > How to parse JAVA's json

How to parse JAVA's json

anonymity
Release: 2019-05-29 09:19:46
Original
4197 people have browsed it

1. What is JSON

JSON is a lightweight data exchange format that uses a text format that is completely independent of programming languages ​​to store and represent data. Simplicity and clear hierarchical structure make JSON an ideal data exchange language. It is easy to read and write, as well as easy to parse and generate, and effectively improves network transmission efficiency.

How to parse JAVA's json

2. JSON syntax

(1) Data is in name/value pairs

(2) Data is separated by commas

(3) Braces save objects

(4) Square brackets save arrays

3. Four ways to generate and parse JSON in Java (simple Demo):

An entity class: used to convert JSON data to and from

public class Person {
    private String name;
    private String sex;
    private int age;
    public Person(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public String getSex() {
        return sex;
    }
    public int getAge() {
        return age;
    }
    @Override
    public String toString() {
        return "Person{name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}';
    }
}
Copy after login

(1) Using the traditional method:

Generate Json string from the entity class :

public Person getPerson(){
    return new Person("张三", "男", 25);
}
@Test
public void EntityToJson(){
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("name", getPerson().getName());
    jsonObject.put("sex", getPerson().getSex());
    jsonObject.put("age", getPerson().getAge());
    System.out.println(jsonObject.toString());
}
Copy after login

Generate entity class from Json string:

@Test
public void JsonToEntity(){
    String jsonString = "{\"sex\":\"男\",\"name\":\"张三\",\"age\":25}";
    JSONObject jsonObject = new JSONObject(jsonString);
    Person person = new Person(jsonObject.get("name").toString(), jsonObject.get("sex").toString(), Integer.valueOf(jsonObject.get("age").toString()));
    System.out.println(person.toString());
}
Copy after login

(2) Use Jackson method:

Generate Json string from entity class:

@Test
public void EntityToJson() throws IOException {
    Person person = new Person("张三", "男", 25);
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = objectMapper.writeValueAsString(person);
    System.out.println(jsonString);
}
Copy after login

Generate entity class from Json string:

@Test
public void JsonToEntity() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = "{\"name\":\"张三\",\"sex\":\"男\",\"age\":25}";
    Person person = objectMapper.readValue(jsonString, Person.class);
    System.out.println(person);
}
Copy after login

(3) Use GSON method:

Generate Json string from entity class:

@Test
public void EntityToJson(){
    Person person = new Person("张三", "男", 25);
    Gson gson = new Gson();
    String jsonString = gson.toJson(person);
    System.out.println(jsonString);
}
Copy after login

Generate entity class from Json string:

@Test
public void JsonToEntity(){
    String jsonString = "{\"name\":\"张三\",\"sex\":\"男\",\"age\":25}";
    Gson gson = new Gson();
    Person person = gson.fromJson(jsonString, Person.class);
    System.out.println(person.toString());
}
Copy after login

(4) Use FastJSON method

Generate Json string from entity class:

@Test
public void EntityToJson(){
    Person person = new Person("张三", "男", 25);
    Object jsonString = JSON.toJSON(person);
    System.out.println(jsonString.toString());
}
Copy after login

Generate entity class from Json string:

@Test
public void JsonToEntity(){
    String jsonString = "{\"name\":\"张三\",\"sex\":\"男\",\"age\":25}";
    Person person = JSON.parseObject(jsonString, Person.class);
    System.out.println(person.toString());
}
Copy after login

The above is the detailed content of How to parse JAVA's json. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template