Home >Backend Development >PHP Tutorial >How to parse JSON data in php

How to parse JSON data in php

青灯夜游
青灯夜游Original
2018-11-15 11:11:5235311browse

How does php parse JSON data? This article will introduce you to the basic method of parsing JSON data in PHP, that is, encoding and decoding JSON data. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First of all, let’s understandWhat is JSON?

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: is defined as a collection of key/value pairs (i.e. key:value). Each object starts with an opening brace "{" and ends with It ends with a right curly bracket "}", and 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 may 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"
    ]}

It can be seen that the data structure of JSON is very similar to that of PHP arrays. Let's learn how php parses JSON data?

PHP has built-in functions that can be used to encode and decode JSON data. These functions are json_encode() function and json_decode() function. Note: These two functions only apply to UTF-8 encoded string data.

Encoding JSON data in PHP

In PHP, the json_encode() function is used to encode values ​​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:

<?php
  // 关联数组
  $marks = array("Peter"=>65, "Harry"=>80, "John"=>78, "Clark"=>90);
   echo json_encode($marks);
?>

The output of the above example will look like this:

How to parse JSON data in php

Similarly, you can encode PHP The index array is encoded as a JSON array, for example:

<?php
  // 索引数组
  $colors = array("红", "绿", "蓝", "橙", "黄");
  echo json_encode($colors);
?>

The output of the above example will look like this:

How to parse JSON data in php

You can also force the json_encode() function using the JSON_FORCE_OBJECT option Return the PHP index array as a JSON object, as shown in the following example:

<?php
  // 索引数组
  $colors = array("红", "绿", "蓝", "橙");
  echo json_encode($colors, JSON_FORCE_OBJECT);
?>

The output of the above example will look like this:

How to parse JSON data in php

As introduced in the above example Yes, non-associative arrays can be encoded as arrays or objects. However, associative arrays are always encoded as objects.

Decoding JSON data with PHP

Decoding JSON data is as easy as encoding it. You can use the PHP json_decode() function to convert a JSON-encoded string to the appropriate PHP data type. The following example demonstrates how to decode or convert a JSON object into a PHP object.

<?php
// 在PHP变量中存储JSON数据
$json = &#39;{"Peter":65,"Harry":80,"John":78,"Clark":90}&#39;;
 
var_dump(json_decode($json));
?>

The output of the above example will look like this:

How to parse JSON data in php

By default, the json_decode() function will return an object. However, you can also choose to specify the second parameter $assoc, which will accept a boolean value that will be decoded into an associative array when the JSON object is set to true; false is the default value. Let’s look at an example:

<?php
// 在PHP变量中存储JSON数据
$json = &#39;{"Peter":65,"Harry":80,"John":78,"Clark":90}&#39;;
 
var_dump(json_decode($json, true));
?>

The output of the above example will look like this:

How to parse JSON data in php

Now let’s look at another example which will show you How to decode JSON data and access individual elements of JSON object or array in PHP.

<?php
// 将JSON编码的字符串分配给PHP变量
$json = &#39;{"Peter":65,"Harry":80,"John":78,"Clark":90}&#39;;
 
// 将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
?>

The output of the above example will look like this:

How to parse JSON data in php

You can also use foreach() to loop through the decoded data like this:

<?php
// 将JSON编码的字符串分配给PHP变量
$json = &#39;{"Peter":65,"Harry":80,"John":78,"Clark":90}&#39;;
 
// 将JSON数据解码为PHP关联数组
$arr = json_decode($json, true);
 
// 通过关联数组循环
foreach($arr as $key=>$value){
    echo $key . "=>" . $value . "<br>";
}
echo "<hr>";
// 将JSON数据解码为PHP对象
$obj = json_decode($json);
 
// 通过对象循环
foreach($obj as $key=>$value){
    echo $key . "=>" . $value . "<br>";
}
?>

The output of the above example will be as follows:

How to parse JSON data in php

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

The above is the detailed content of How to parse JSON data in php. 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