Current Location: Home> Latest Articles> PHP and Java JSON Conversion Example: PHP Array to JSON and Java Object to JSON Operation

PHP and Java JSON Conversion Example: PHP Array to JSON and Java Object to JSON Operation

gitbox 2025-06-25

1. PHP Array to JSON

In PHP, converting an array to a JSON format is quite simple. PHP provides the json_encode()

In the code above, we define a PHP array $phpArray containing key-value pairs for name (name), age (age), and city (city). Then we use the json_encode() function to convert the PHP array to a JSON string and assign the result to the $jsonString variable. Finally, we output the JSON string using the echo statement.

The output of the code will be:

{"name":"John","age":30,"city":"New York"}

Key Point

By using the json_encode() function, we can easily convert a PHP array into a valid JSON string. This makes it easier to transmit PHP arrays to other programming languages or store them in a database.

2. Java JSON Data Format Conversion

In Java, we can use third-party libraries to convert JSON data. Below, we use the Jackson library as an example to demonstrate how to convert a JSON string into a Java object.

1) Add Jackson Library

First, we need to add the Jackson library dependencies to our project. Assuming we're using Maven to manage the project, add the following dependency to the pom.xml file:


<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.4</version>
</dependency>

2) Convert JSON String to Java Object

The following example shows how to convert a JSON string into a Java object:


import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonExample {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Person person = objectMapper.readValue(jsonString, Person.class);

            System.out.println(person.getName());
            System.out.println(person.getAge());
            System.out.println(person.getCity());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Person {
    private String name;
    private int age;
    private String city;

    // Getters and Setters
    public String getName() { return name; }
    public int getAge() { return age; }
    public String getCity() { return city; }
}

In the code above, we first define a JSON string jsonString that contains name (name), age (age), and city (city). Then, we use the ObjectMapper class’s readValue() method to convert the JSON string into a Java object Person. Finally, we use getXXX() methods to retrieve values from the Java object and print them.

The output of the code will be:

John
30
New York

Key Point

In Java, we can use third-party libraries (such as the Jackson library) to perform bidirectional conversion between JSON strings and Java objects. By converting JSON data into Java objects, we can easily manipulate JSON data using Java methods and properties.

Conclusion

This article explained how to perform JSON data format conversion in both PHP and Java. In PHP, we can use the json_encode() function to convert a PHP array into a JSON string. In Java, we can use third-party libraries (such as the Jackson library) to convert between JSON strings and Java objects.

Using JSON allows easy data exchange and manipulation between different programming languages. It has become a widely used data format for web development, and for developers using PHP and Java, mastering JSON conversion and manipulation is crucial.