Home Java javaTutorial Understand the serialization and deserialization technology of JSON arrays in Java.

Understand the serialization and deserialization technology of JSON arrays in Java.

Sep 06, 2023 pm 12:37 PM
Serialization Deserialization json array

Understand the serialization and deserialization technology of JSON arrays in Java.

Understand the serialization and deserialization technology of JSON arrays in Java

In today's software development, data transmission and storage is a very common requirement. JSON (JavaScript Object Notation), as a lightweight data exchange format, is widely used in various programming languages. In Java, we often need to serialize and deserialize JSON for use in network transmission or storage. For arrays in JSON, Java provides some convenient techniques for operations.

In Java, there are many libraries that can be used for JSON serialization and deserialization. This article will take the commonly used Jackson library as an example to introduce how to serialize and deserialize JSON arrays.

  1. Serialization of JSON array

During the serialization process, we convert the Java object into a string in JSON format. For serialization of JSON array, we need to convert Java array or collection object to JSON format array.

First, we need to introduce dependencies on the Jackson library. You can use maven to manage dependencies:

1

2

3

4

5

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.12.4</version>

</dependency>

Copy after login

Next, we define a Java class to represent a Person object:

1

2

3

4

5

6

public class Person {

    private String name;

    private int age;

 

    // getter and setter methods

}

Copy after login

Then, we create an array containing Person objects and use the ObjectMapper class to convert it into a string in JSON format:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

import com.fasterxml.jackson.databind.ObjectMapper;

 

import java.util.Arrays;

 

public class JsonArraySerializationExample {

    public static void main(String[] args) throws Exception {

        Person person1 = new Person();

        person1.setName("Alice");

        person1.setAge(25);

 

        Person person2 = new Person();

        person2.setName("Bob");

        person2.setAge(30);

 

        Person[] persons = {person1, person2};

 

        ObjectMapper objectMapper = new ObjectMapper();

        String json = objectMapper.writeValueAsString(persons);

 

        System.out.println(json);

    }

}

Copy after login

Run the above code and the output is as follows:

1

[{"name":"Alice","age":25},{"name":"Bob","age":30}]

Copy after login

You can see At this point, we successfully converted the array of Person objects into a JSON-formatted string.

  1. Deserialization of JSON array

During the deserialization process, we convert the JSON-formatted string into a Java object. For deserialization of JSON array, we convert JSON array to Java array or collection object.

We use the above Person class as an example and create a JSON-formatted string containing the Person object. Then use the ObjectMapper class to convert it into an array of Person objects:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import com.fasterxml.jackson.databind.ObjectMapper;

 

import java.util.Arrays;

 

public class JsonArrayDeserializationExample {

    public static void main(String[] args) throws Exception {

        String json = "[{"name":"Alice","age":25},{"name":"Bob","age":30}]";

 

        ObjectMapper objectMapper = new ObjectMapper();

        Person[] persons = objectMapper.readValue(json, Person[].class);

 

        for (Person person : persons) {

            System.out.println(person.getName() + ", " + person.getAge());

        }

    }

}

Copy after login

Run the above code, the output is as follows:

1

2

Alice, 25

Bob, 30

Copy after login

As you can see, we successfully converted the JSON formatted string into Array of Person objects.

Summary

This article introduces the serialization and deserialization technology of JSON arrays in Java. By using the Jackson library, we can easily convert an array of Java objects into a JSON-formatted string for use in network transmission or storage. At the same time, we can also convert JSON format strings into Java object arrays to facilitate subsequent data operations. I hope this article will help you understand the serialization and deserialization technology of JSON arrays in Java.

The above is the detailed content of Understand the serialization and deserialization technology of JSON arrays in Java.. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of sorting and filtering operations of JSON arrays in Java. Detailed explanation of sorting and filtering operations of JSON arrays in Java. Sep 06, 2023 pm 03:22 PM

Detailed explanation of sorting and filtering operations of JSON arrays in Java In Java development, processing JSON data is a common task. As one of the commonly used data structures, JSON array often requires sorting and filtering operations in practical applications. This article will introduce in detail the sorting and filtering operations of JSON arrays in Java and provide corresponding code examples. 1. Sorting operation of JSON array: Use JSONArray object to store JSON array in Java, and use json library to process JSON data

Get started quickly: JSON array merging and splitting techniques in Java. Get started quickly: JSON array merging and splitting techniques in Java. Sep 06, 2023 am 10:21 AM

Get started quickly: JSON array merging and splitting techniques in Java In modern software development, data format and transmission have become increasingly important. Among them, JSON (JavaScriptObjectNotation) is a commonly used data format, especially suitable for front-end and back-end interaction and data storage. In Java development, we often need to deal with JSON objects and JSON arrays. This article explains how to merge and split JSON arrays in Java, along with tips and examples for implementing these operations.

How to solve php deserialization failure How to solve php deserialization failure Oct 11, 2023 am 09:30 AM

Solution to PHP deserialization failure Check the serialized data. Check class definitions, check error logs, update PHP versions and apply security measures, etc. Detailed introduction: 1. Check the serialized data. First check whether the serialized data is valid and conforms to PHP's serialization specification. If the data is damaged or has an incorrect format, you can try to repair it or restore the correct data from backup; 2. Check Class definition, ensure that all classes used in serialized data exist and can be automatically loaded. If the class does not exist or is inaccessible, you can try to repair the class definition, etc.

How to convert JSON array to CSV in Java? How to convert JSON array to CSV in Java? Aug 21, 2023 pm 08:27 PM

JSON can be used as a data exchange format, it is lightweight and language independent. A JSONArray can parse text strings to produce vector-like objects and supports the java.util.List interface. We can convert JSON array to CSV format using org.json.CDL class, which provides a static method toString() for converting JSONArray to comma-separated text. We need to import the org.apache.commons.io.FileUtils package to store data in a CSV file using the writeStringToFile() method. Syntaxpublicstaticj

In Java, how can we serialize a list of objects using flexjson? In Java, how can we serialize a list of objects using flexjson? Sep 05, 2023 pm 11:09 PM

Flexjson is a lightweight library for serializing and deserializing Java objects to and from JSON format. We can serialize a list of objects using the serialize() method of the JSONSerializer class. This method performs shallow serialization on the target instance. We need to pass a list of objects of list type as a parameter to the serialize() method. Syntax publicStringserialize(Objecttarget) example importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerial

How does Java serialization affect performance? How does Java serialization affect performance? Apr 16, 2024 pm 06:36 PM

The impact of serialization on Java performance: The serialization process relies on reflection, which will significantly affect performance. Serialization requires the creation of a byte stream to store object data, resulting in memory allocation and processing costs. Serializing large objects consumes a lot of memory and time. Serialized objects increase load when transmitted over the network.

PHP data processing skills: How to use the serialize and unserialize functions to implement data serialization and deserialization PHP data processing skills: How to use the serialize and unserialize functions to implement data serialization and deserialization Jul 29, 2023 am 10:49 AM

PHP data processing skills: How to use the serialize and unserialize functions to implement data serialization and deserialization Serialization and deserialization are one of the commonly used data processing skills in computer science. In PHP, we can use the serialize() and unserialize() functions to implement data serialization and deserialization operations. This article will give you a detailed introduction to how to use these two functions and provide relevant code examples. 1. What is serialization and deserialization in computer programming?

How does the C++ function library perform serialization and deserialization? How does the C++ function library perform serialization and deserialization? Apr 18, 2024 am 10:06 AM

C++ Library Serialization and Deserialization Guide Serialization: Creating an output stream and converting it to an archive format. Serialize objects into archive. Deserialization: Creates an input stream and restores it from archive format. Deserialize objects from the archive. Practical example: Serialization: Creating an output stream. Create an archive object. Create and serialize objects into the archive. Deserialization: Create an input stream. Create an archive object. Create objects and deserialize them from the archive.

See all articles