How to Parse JSON Data in PHP: Encoding and Decoding Guide
This article explains what JSON is and demonstrates how to encode and decode JSON data in PHP using json_encode() and json_decode(), including examples with associative arrays, indexed arrays, JSON_FORCE_OBJECT, and iteration over decoded structures.
How to parse JSON data in PHP? This article introduces the basic methods for encoding and decoding JSON data in PHP, providing useful reference for developers who need to work with JSON.
First, let’s understand what JSON is
JSON is a standard lightweight data‑exchange format that can be parsed and generated quickly and easily.
Like XML, JSON is a text‑based format that is easy to write and read, but unlike XML it uses less bandwidth. JSON is built on two basic structures:
Object: a collection of key/value pairs enclosed in curly braces { and } , with pairs separated by commas , .
Array: an ordered list of values enclosed in square brackets [ and ] , with values separated by commas , .
In JSON, keys are always strings, while values can be strings, numbers, true, false, null, objects, or arrays. Strings must be quoted with double quotes and may contain escape characters such as \n , \t , and \\ .
<code>{
"book": {
"name": "PHP 从入门到精通",
"author": "明日科技",
"year": 2017,
"type": "php编程",
"bestseller": true
}
}
</code>An example of a JSON array:
<code>{
"fruits": [
"Apple",
"Banana",
"Strawberry",
"Mango"
]
}
</code>Now let’s see how PHP parses JSON data
PHP provides built‑in functions for JSON encoding and decoding: json_encode() and json_decode() . These functions work only with UTF‑8 encoded strings.
Encoding JSON data in PHP
The json_encode() function converts a PHP value to a JSON string. Any type except resources can be encoded. The following example shows how to encode an associative array as a JSON object:
<code><?php
// associative array
$marks = array("Peter"=>65, "Harry"=>80, "John"=>78, "Clark"=>90);
echo json_encode($marks);
?>
</code>The output is a JSON object representing the marks.
You can also encode an indexed array as a JSON array:
<code><?php
// indexed array
$colors = array("红", "绿", "蓝", "橙", "黄");
echo json_encode($colors);
?>
</code>Using the JSON_FORCE_OBJECT option forces json_encode() to output an object even for indexed arrays:
<code><?php
// indexed array
$colors = array("红", "绿", "蓝", "橙");
echo json_encode($colors, JSON_FORCE_OBJECT);
?>
</code>Non‑associative arrays can be encoded as either arrays or objects, but associative arrays are always encoded as objects.
Decoding JSON data with PHP
Decoding JSON is straightforward with json_decode() , which converts a JSON string into the appropriate PHP type. The following example decodes a JSON object into a PHP object:
<code><?php
// store JSON string in a variable
$json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}';
var_dump(json_decode($json));
?>
</code>By default, json_decode() returns an object. Passing true as the second argument returns an associative array instead:
<code><?php
$json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}';
var_dump(json_decode($json, true));
?>
</code>After decoding, you can access individual elements directly:
<code><?php
$json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}';
$arr = json_decode($json, true);
echo $arr["Peter"]; // 65
echo $arr["Harry"]; // 80
echo $arr["John"]; // 78
echo $arr["Clark"]; // 90
$obj = json_decode($json);
echo $obj->Peter; // 65
echo $obj->Harry; // 80
echo $obj->John; // 78
echo $obj->Clark; // 90
?>
</code>You can also iterate over decoded data with foreach() for both arrays and objects:
<code><?php
$json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}';
$arr = json_decode($json, true);
foreach($arr as $key => $value){
echo $key . "=>" . $value . "<br>";
}
echo "<hr>";
$obj = json_decode($json);
foreach($obj as $key => $value){
echo $key . "=>" . $value . "<br>";
}
?>
</code>This article has covered the fundamentals of JSON handling in PHP, including encoding, decoding, options, and iteration techniques.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.