Home >Backend Development >PHP Problem >Let's analyze how PHP parses JSON data
This article brings you relevant knowledge about PHP, which mainly introduces how to parse JSON data in PHP. JSON is a standard lightweight data exchange format, which can It is quick and easy to parse and generate. Let’s take a look at it. I hope it will be helpful to everyone.
Recommended study: "PHP Video Tutorial"
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 easy to understand, but unlike XML, the JSON data structure takes up less bandwidth than the XML version. JSON is based on two basic structures:
Object: It is defined as a collection of key/value pairs (i.e. key:value). Each object starts with an opening brace "{" and ends with a closing brace "}" At the end, multiple key/value pairs are separated by commas ",".
Array: is defined as an ordered list of values. The array starts with a left bracket "[" and ends with a right bracket "]". The values are separated by commas ",".
In JSON, the key is always a string, and the value can be string, number, true or false, null or even object or array. Strings must be enclosed in double quotes and can contain escape characters such as \n, \t, and \. A JSON object might look like this:
{ "book": { "name": "PHP 从入门到精通", "author": "明日科技", "year": 2017, "type": "php编程", "bestseller": true } }
And an example of a JSON array looks like this:
{ "fruits": [ "Apple", "Banana", "Strawberry", "Mango" ] }
In PHP , the json_encode() function is used to encode the value into JSON format. The encoded value can be any PHP data type except resources, such as database or file handles. The following example demonstrates how to encode a PHP associative array into a JSON object:
// 关联数组 $marks = array("Peter"=>65, "Harry"=>80, "John"=>78, "Clark"=>90); echo json_encode($marks);
Similarly, you can encode a PHP index array into a JSON array, for example:
// 索引数组 $colors = array("红", "绿", "蓝", "橙", "黄"); echo json_encode($colors);
You can also use the JSON_FORCE_OBJECT option to force The json_encode() function returns a PHP index array as a JSON object, as shown in the following example:
// 索引数组 $colors = array("红", "绿", "蓝", "橙"); echo json_encode($colors, JSON_FORCE_OBJECT);
Decode JSON data just like encoding it Simple. You can use the PHP json_decode() function to convert a JSON-encoded string to the appropriate PHP data type.
Now let us look at an example that will show you how to decode JSON data and access individual elements of a JSON object or array in PHP.
<?php // 将JSON编码的字符串分配给PHP变量 $json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}'; // 将JSON数据解码为PHP关联数组 $arr = json_decode($json, true); // Access values from the associative array echo $arr["Peter"]; // Output: 65 echo $arr["Harry"]; // Output: 80 echo $arr["John"]; // Output: 78 echo $arr["Clark"]; // Output: 90 // 将JSON数据解码为PHP对象 $obj = json_decode($json); // 返回对象的访问值 echo $obj->Peter; // Output: 65 echo $obj->Harry; // Output: 80 echo $obj->John; // Output: 78 echo $obj->Clark; // Output: 90 ?>
You can also use foreach() to loop through the decoded data, as shown below:
<?php // 将JSON编码的字符串分配给PHP变量 $json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}'; // 将JSON数据解码为PHP关联数组 $arr = json_decode($json, true); // 通过关联数组循环 foreach($arr as $key=>$value){ echo $key . "=>" . $value . ""; } echo ""; // 将JSON数据解码为PHP对象 $obj = json_decode($json); // 通过对象循环 foreach($obj as $key=>$value){ echo $key . "=>" . $value . ""; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Let's analyze how PHP parses JSON data. For more information, please follow other related articles on the PHP Chinese website!