Home  >  Article  >  Backend Development  >  How PHP parses JSON data

How PHP parses JSON data

PHPz
PHPzOriginal
2023-04-20 10:15:092925browse

JSON (JavaScript Object Notation) is a lightweight data exchange format. It uses human-readable text to transfer and store data objects. Unlike XML, JSON is easier to parse and process, so it is often used when transferring and exchanging data between web applications and servers.

In PHP, you can use the built-in json_decode function to convert a JSON string into a PHP object. For example, the following example parses a JSON string into a PHP object:

<?php
$json_string = &#39;{"name":"John", "age":30, "city":"New York"}&#39;;
 
$obj = json_decode($json_string);
 
echo $obj->name; //输出 John
echo $obj->age; //输出 30
echo $obj->city; //输出 New York
?>

This function accepts two parameters: the JSON string to be parsed and a Boolean variable indicating to convert the parsed JSON object into a PHP object ( Default is false) or associative array (true).

However, if the JSON data contains an array of objects or an array of objects, it will need to be processed using a recursive function. Here is an example where the JSON data contains nested object arrays and object arrays:

{
    "employees": [
        {
            "name": "John Doe",
            "email": "john@example.com",
            "phones": [
                {
                    "type": "home",
                    "number": "555-555-1234"
                },
                {
                    "type": "work",
                    "number": "555-555-5678"
                }
            ]
        },
        {
            "name": "Jane Smith",
            "email": "jane@example.com",
            "phones": [
                {
                    "type": "home",
                    "number": "555-555-4321"
                },
                {
                    "type": "work",
                    "number": "555-555-8765"
                }
            ]
        }
    ]
}

To parse such data, you can write a recursive function that iterates through the entire JSON object and converts it to a PHP object or array. Here is an example function that handles JSON object arrays, object arrays, and standard JSON objects:

 $value) {
        if (is_object($value)) {
            $result[$key] = json_to_array($value);
        } else if (is_array($value)) {
            $result[$key] = [];
 
            foreach ($value as $item) {
                $result[$key][] = json_to_array($item);
            }
        } else {
            $result[$key] = $value;
        }
    }
 
    return $result;
}
 
$json_string = '{
    "employees": [
        {
            "name": "John Doe",
            "email": "john@example.com",
            "phones": [
                {
                    "type": "home",
                    "number": "555-555-1234"
                },
                {
                    "type": "work",
                    "number": "555-555-5678"
                }
            ]
        },
        {
            "name": "Jane Smith",
            "email": "jane@example.com",
            "phones": [
                {
                    "type": "home",
                    "number": "555-555-4321"
                },
                {
                    "type": "work",
                    "number": "555-555-8765"
                }
            ]
        }
    ]
}';
 
$obj = json_decode($json_string);
$array = json_to_array($obj);
 
print_r($array);
?>

This function will return a PHP array containing the JSON data for all nested object arrays and objects.

The above is the detailed content of How PHP parses JSON data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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