


How to parse and traverse JSON array in JAVA? Master JSON array processing skills.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Win11 Tips Sharing: One trick to skip Microsoft account login Windows 11 is the latest operating system launched by Microsoft, with a new design style and many practical functions. However, for some users, having to log in to their Microsoft account every time they boot up the system can be a bit annoying. If you are one of them, you might as well try the following tips, which will allow you to skip logging in with a Microsoft account and enter the desktop interface directly. First, we need to create a local account in the system to log in instead of a Microsoft account. The advantage of doing this is

We often create and edit tables in excel, but as a novice who has just come into contact with the software, how to use excel to create tables is not as easy as it is for us. Below, we will conduct some drills on some steps of table creation that novices, that is, beginners, need to master. We hope it will be helpful to those in need. A sample form for beginners is shown below: Let’s see how to complete it! 1. There are two methods to create a new excel document. You can right-click the mouse on a blank location on the [Desktop] - [New] - [xls] file. You can also [Start]-[All Programs]-[Microsoft Office]-[Microsoft Excel 20**] 2. Double-click our new ex

In C language, it represents a pointer, which stores the address of other variables; & represents the address operator, which returns the memory address of a variable. Tips for using pointers include defining pointers, dereferencing pointers, and ensuring that pointers point to valid addresses; tips for using address operators & include obtaining variable addresses, and returning the address of the first element of the array when obtaining the address of an array element. A practical example demonstrating the use of pointer and address operators to reverse a string.

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

VSCode (Visual Studio Code) is an open source code editor developed by Microsoft. It has powerful functions and rich plug-in support, making it one of the preferred tools for developers. This article will provide an introductory guide for beginners to help them quickly master the skills of using VSCode. In this article, we will introduce how to install VSCode, basic editing operations, shortcut keys, plug-in installation, etc., and provide readers with specific code examples. 1. Install VSCode first, we need

Title: PHP Programming Tips: How to Jump to a Web Page within 3 Seconds In web development, we often encounter situations where we need to automatically jump to another page within a certain period of time. This article will introduce how to use PHP to implement programming techniques to jump to a page within 3 seconds, and provide specific code examples. First of all, the basic principle of page jump is realized through the Location field in the HTTP response header. By setting this field, the browser can automatically jump to the specified page. Below is a simple example demonstrating how to use P

Win11 tricks revealed: How to bypass Microsoft account login Recently, Microsoft launched a new operating system Windows11, which has attracted widespread attention. Compared with previous versions, Windows 11 has made many new adjustments in terms of interface design and functional improvements, but it has also caused some controversy. The most eye-catching point is that it forces users to log in to the system with a Microsoft account. For some users, they may be more accustomed to logging in with a local account and are unwilling to bind their personal information to a Microsoft account.

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu
