How to convert JSON array to Java object in Java?
JSON (JavaScript Object Notation) is a lightweight data exchange format that is widely used in front-end and back-end data transmission and storage. In Java, we usually use third-party libraries to parse and build JSON data. This article will introduce how to use the JSON library to convert a JSON array into a Java object and provide corresponding code examples.
Before processing JSON data, we need to introduce the JSON library first. Here we use Google's Gson library, which is an open source and powerful Java JSON library. You can introduce the Gson library by adding the following dependency in pom.xml (Maven) or build.gradle (Gradle):
<!-- Maven --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
// Gradle implementation 'com.google.code.gson:gson:2.8.6'
Next, we will provide a simple JSON array example:
[ { "name": "John", "age": 25, "city": "New York" }, { "name": "Alice", "age": 28, "city": "London" }, { "name": "Bob", "age": 30, "city": "Tokyo" } ]
The following are the steps and code examples to convert a JSON array to a Java object:
1. Create a Java class
First, we need to create a Java class to Represents data in a JSON array. Each object should contain fields corresponding to JSON keys. In this example, we create a Java class named Person, containing name, age and city fields:
public class Person { private String name; private int age; private String city; // getter and setter methods }
2. Parse the JSON array
Next, We will parse JSON array by using Gson library. First, we need to create a Gson object:
Gson gson = new Gson();
Then, we can use the fromJson() method to convert the JSON array into an array of Java objects:
String json = "[{...}, {...}, {...}]"; // JSON数组字符串 Person[] persons = gson.fromJson(json, Person[].class);
In this way, each element in the persons array Is a Person object that can be accessed by index.
3. Using Java objects
Now, we can easily access and use the properties of the Person object:
for (Person person : persons) { System.out.println(person.getName()); System.out.println(person.getAge()); System.out.println(person.getCity()); }
The above is to convert the JSON array Complete code example for Java objects:
import com.google.gson.Gson; public class JsonArrayToObjectExample { public static void main(String[] args) { String json = "[{"name":"John","age":25,"city":"New York"},{"name":"Alice","age":28,"city":"London"},{"name":"Bob","age":30,"city":"Tokyo"}]"; Gson gson = new Gson(); Person[] persons = gson.fromJson(json, Person[].class); for (Person person : persons) { System.out.println(person.getName()); System.out.println(person.getAge()); System.out.println(person.getCity()); } } } class Person { private String name; private int age; private String city; 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 getCity() { return city; } public void setCity(String city) { this.city = city; } }
Through the above steps, we can convert JSON arrays into Java objects and use these objects conveniently in the code. Using the Gson library can help us simplify the parsing process of JSON data and improve the readability and maintainability of the code.
Hope this article can help you understand how to convert JSON array to Java object in Java!
The above is the detailed content of How to convert JSON array to Java object in Java?. For more information, please follow other related articles on the PHP Chinese website!