Home Backend Development PHP Problem How PHP parses JSON data

How PHP parses JSON data

Apr 20, 2023 am 10:15 AM

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
?>
Copy after login

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

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

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 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24