How to parse and traverse JSON arrays in JAVA? Master JSON array processing skills.
With the rapid development of the modern Internet, JSON (JavaScript Object Notation) has become a commonly used data exchange format. It is concise, easy to read, and very suitable for data transmission in web development and API interfaces. In JAVA, parsing and traversing JSON arrays are very common operations. This article will introduce how to use JAVA to parse JSON arrays and give corresponding code examples.
First, we need to import the relevant libraries to process JSON. In JAVA, you can use some third-party libraries, such as Jackson, Gson, etc. Here, we take the Jackson library as an example to introduce. First, add the corresponding dependencies to your project:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency>
The following is a simple JSON array example:
[ { "name": "Alice", "age": 25 }, { "name": "Bob", "age": 28 }, { "name": "Charlie", "age": 30 } ]
Next, let’s take a look at how to use JAVA to parse and traverse this JSON array.
First, we need to define a POJO class (Plain Old Java Object) to map the data in JSON. For the above JSON array example, we can define a Person
class:
public class Person { private String name; private int age; // 省略getter和setter方法 }
We can then use the following code to parse the JSON array:
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; public class JsonArrayParsingExample { public static void main(String[] args) { String json = "[{"name":"Alice","age":25},{"name":"Bob","age":28},{"name":"Charlie","age":30}]"; ObjectMapper objectMapper = new ObjectMapper(); try { Person[] persons = objectMapper.readValue(json, Person[].class); // 遍历数组 for (Person person : persons) { System.out.println("Name: " + person.getName() + ", Age: " + person.getAge()); } } catch (IOException e) { e.printStackTrace(); } } }
First, we create An instance of ObjectMapper
is created, which is the core class of the Jackson library and is used to process JSON. We then use the readValue
method to convert the JSON string into an array of Person
objects. Finally, we iterate through the array and print out each person's name and age.
The above code output:
Name: Alice, Age: 25 Name: Bob, Age: 28 Name: Charlie, Age: 30
If each object in the JSON array has a different structure, we can use the JsonNode
object to process it. JsonNode
is an abstract class used to represent JSON nodes in the Jackson library. The following is an example of processing JSON arrays of different structures:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; public class JsonArrayParsingExample { public static void main(String[] args) { String json = "[{"name":"Alice","age":25},{"title":"Software Engineer","salary":5000}]"; ObjectMapper objectMapper = new ObjectMapper(); try { JsonNode[] nodes = objectMapper.readValue(json, JsonNode[].class); // 遍历数组 for (JsonNode node : nodes) { // 判断节点类型 if (node.has("name")) { String name = node.get("name").asText(); int age = node.get("age").asInt(); System.out.println("Name: " + name + ", Age: " + age); } else if (node.has("title")) { String title = node.get("title").asText(); int salary = node.get("salary").asInt(); System.out.println("Title: " + title + ", Salary: " + salary); } } } catch (IOException e) { e.printStackTrace(); } } }
Output:
Name: Alice, Age: 25 Title: Software Engineer, Salary: 5000
The above is a simple example of parsing and traversing JSON arrays in JAVA. Mastering these basic skills, you can easily process the JSON data returned from the server, extract the required information for further processing and display. Hope this article is helpful to you!
The above is the detailed content of How to parse and traverse JSON array in JAVA? Master JSON array processing skills.. For more information, please follow other related articles on the PHP Chinese website!