JSON (JavaScript Object Notation) is a lightweight data interchange format.
In brief, both XML and JSON are used to facilitate the transfer of data between client and server, especially for object-type data, such as the most common arrays.
The following will send the array from PHP to JavaScript separately, and the array from JavaScript to the PHP sample description, the example is relatively simple, understand the concept can be. Whether it is sent from PHP to JavaScript or JavaScript to Php,json, the object is flattened and then transformed into a string before being transmitted.
PHP value to JavaScript
PHP file json.php
' Shia ', ' nick ' = ' gonn ', ' contact ' + = array (' email ' = ' gonnsai@163.com ', ' website ' = ' http://www.bkjia.com ', ); $json _string = Json_encode ($arr); echo "GetProfile ($json _string)";? >
The results of this file are as follows:
GetProfile ({"Name": "u5e0cu4e9a", "Nick": "Gonn", "contact": {"email": "gonnsai@163.com", "website": "Http://www.bkjia.com"}})
Json.php is a json_encode function that flattens an array and then sends it, instead of a json_decode function.
So how do you call it in JavaScript? It's very simple to define a variable to get the JSON from PHP, which has the properties of the object, and we can get the properties of the JSON in Array.name this way.
The results of the operation are as follows:
Shia gonngonnsai@163.comhttp://www.bkjia.com
JavaScript passes value to PHP
Json_encode.html
Json:from JavaScript to PHP
Here JavaScript flattening requires a plug-in: Http://www.json.org/json2.js, flattening the object through Json.stringify (str) and then passing it to PHP.
Note: There is another http://www.json.org/json.js, corresponding to the toJSONString method.
var last=obj.tojsonstring (); For Json.jsvar last=json.stringify (obj); For Json2.js
json_encode.php
' Echo $user->name. '
' Echo $user->email. '
' Echo $user->password. '
';? >
Here you need to use the Json_decode () function, and then call the data with the $obj-so property.
http://www.bkjia.com/PHPjc/752487.html www.bkjia.com true http://www.bkjia.com/PHPjc/752487.html techarticle JSON (JavaScript Object Notation) is a lightweight data interchange format. In brief, both XML and JSON are designed to facilitate the interaction of data between the client and the server .