


In web development, PHP and Java are both very common programming languages. In different application scenarios, both languages have considerable advantages. In some projects, we may need to convert PHP arrays into object arrays in Java. This is a very common requirement. Below, we will learn how to achieve this requirement.
- Convert PHP array into JSON string
First, we need to convert PHP array into JSON format string:
$array = array( 'name' => 'John', 'age' => 20, 'gender' => 'male' ); $json_string = json_encode($array); echo $json_string;
Run the above Code, the output is as follows:
{"name":"John","age":20,"gender":"male"}
- Java parsing JSON string
Then, we need to parse the JSON string in Java and convert the JSON string into a Java object :
import com.google.gson.Gson; class User { String name; int age; String gender; public User(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } } public class Main { public static void main(String[] args) { String json_string = "{\"name\":\"John\",\"age\":20,\"gender\":\"male\"}"; Gson gson = new Gson(); User user = gson.fromJson(json_string, User.class); System.out.println(user.name + " " + user.age + " " + user.gender); } }
Run the above code, the output is as follows:
John 20 male
- Convert the PHP array into a Java object array
Next, we convert the PHP array into Java object array:
$array = array( array( 'name' => 'John', 'age' => 20, 'gender' => 'male' ), array( 'name' => 'Tom', 'age' => 21, 'gender' => 'male' ), array( 'name' => 'Lucy', 'age' => 19, 'gender' => 'female' ) ); $json_string = json_encode($array); echo $json_string;
Run the above code, the output is as follows:
[{"name":"John","age":20,"gender":"male"},{"name":"Tom","age":21,"gender":"male"},{"name":"Lucy","age":19,"gender":"female"}]
We need to use the array in Java to receive the objects in the JSON string:
import com.google.gson.Gson; class User { String name; int age; String gender; public User(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } } public class Main { public static void main(String[] args) { String json_string = "[{\"name\":\"John\",\"age\":20,\"gender\":\"male\"},{\"name\":\"Tom\",\"age\":21,\"gender\":\"male\"},{\"name\":\"Lucy\",\"age\":19,\"gender\":\"female\"}]"; Gson gson = new Gson(); User[] users = gson.fromJson(json_string, User[].class); for (User user : users) { System.out.println(user.name + " " + user.age + " " + user.gender); } } }
Run the above The code output is as follows:
John 20 male Tom 21 male Lucy 19 female
So far, we have successfully converted a PHP array into a Java object array. In this way, we can flexibly use PHP and Java in different projects to achieve better web applications.
The above is the detailed content of Let's talk about how to convert a php array into a java object array. 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

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

Hot Article

Hot Tools

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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