Home Backend Development PHP Problem Let's talk about how to convert a php array into a java object array

Let's talk about how to convert a php array into a java object array

Apr 11, 2023 am 10:38 AM

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.

  1. 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;
Copy after login

Run the above Code, the output is as follows:

{"name":"John","age":20,"gender":"male"}
Copy after login
  1. 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);
    }
}
Copy after login

Run the above code, the output is as follows:

John 20 male
Copy after login
  1. 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;
Copy after login

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"}]
Copy after login

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);
        }
    }
}
Copy after login

Run the above The code output is as follows:

John 20 male
Tom 21 male
Lucy 19 female
Copy after login

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!

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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24