PHP JSON format data interaction example code details
- 2020-03-31 21:26:37
- OfStack
Introduction to JSON
JSON(JavaScript Object Notation) is a lightweight data interchange format.
JSON has two main structures:
A collection of name/value pairs, understood in PHP as an associative array.
An ordered list of values. In PHP, it is understood as an ordinary array.
An object is an unordered collection of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with"} "(right parenthesis). Each name is followed by: (colon); "Name/value" pairs are separated by ", "(comma).
An array is an ordered collection of values. An array starts with "[" (left bracket) and ends with"] "(right bracket). Values are separated by ", "(comma). A value can be a string, number, true, false, null, object, or array enclosed in double quotes. These structures can be nested.
My current understanding of JSON is more of an array, similar to associative arrays in PHP, which you can convert to JSON.
The introduction of more JSON refer (link: http://www.json.org/json-zh.html) and (link: http://www.ibm.com/developerworks/cn/web/wa-lo-json/)
PHP JSON parses the instance
JSON has been included as part of the PHP extension since PHP5.2, so you don't need to install it separately. In addition, in order to ensure that the following PHP JSON instance does not appear Chinese garrulous code, please ensure that your encoding format is UTF8.
JSON is part of Javascript, so first let's look at how JSON is defined in Javascript
var json_obj = {'WebName':'PHP Website development tutorial net '};
alert(json_obj.WebName);
var json_arr =[{'WebName':'PHP Website development tutorial net ','WebSite':'//www.jb51.net'},{'ArtTitle':'PHP JSON instance explanation '}]
alert(json_arr[1].ArtTitle);
var php_json = [['PHP Website development tutorial net ','//www.jb51.net'],[' article title ','PHP JSON instance explanation ']];
alert(php_json[1][0]);
In this JSON instance code I've built all the JSON forms,
Line 1 defines that JSON exists as an object
Line 4 defines that JSON exists as an array, nested JSON objects, so it's kind of like an associative array in PHP, but it's still an object.
Line 7 determines that JSON exists as a normal array.
Note: because Javascript is case-sensitive, be aware of the case of keys when accessing JSON associative array objects.
When PHP interacts with json-formatted data, the first step is to convert the PHP array into json-formatted data. The json_encode function provided by PHP5 can be used. When PHP parsed the json-formatted data passed by PHP, it needs to use the json_decode function to parse it into a PHP array
<?php
$json_arr = array('WebName'=>'PHP Website development tutorial net ','WebSite'=>'//www.jb51.net');
$php_json = json_encode($json_arr);
echo $php_json;
$php_json = json_decode($php_json);
print_r($php_json);
?>
instructions : the associative array is defined through PHP, and then converted into JSON data through json_encode. The converted JSON object can be seen through echo statement. Json_decode, the PHP JSON parsing function, can parse the json-formatted data into an associative array of PHP. If the encoding is not correct, JSON Chinese will appear garbled. If you (link: http://www.leapsoul.cn/? P =818), notice that the encoding changes ANSI to UTF8 when saving the file.
Finally, let's look at the complete PHP JSON interaction
<?php
$json_arr = array('WebName'=>'PHP Website development tutorial net ','WebSite'=>'//www.jb51.net');
$php_json = json_encode($json_arr);
?>
<script type="text/javascript">
var php_json = <?=$php_json?>;
</script>
<script type="text/javascript">
function php_json_dis(php_json)
{
alert(php_json.WebName);
alert(php_json.WebSite);
}
php_json_dis(php_json);
</script>
This completes an example of PHP transforming and parsing json-formatted data through the json_encode and json_decode functions.