Home  >  Article  >  Backend Development  >  Detailed explanation of the method of data exchange between PHP and JavaScript using Json_PHP Tutorial

Detailed explanation of the method of data exchange between PHP and JavaScript using Json_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:04:531233browse

JSON (JavaScript Object Notation) is a lightweight data exchange format.
In short, both xml and json are transfer stations to facilitate the exchange of data between the client and the server, especially for object-type data, such as the most common arrays.

The following are examples of transferring arrays from php to javascript, and transferring arrays from javascript to php. The examples are relatively simple, and you only need to understand the concept. Regardless of whether it is transmitted from php to javascript or javascript to php, json will flatten the object before transmitting it, that is, one-dimensionally convert it into a string.
PHP passes value to JavaScript
PHP file json.php

Copy code The code is as follows :

$arr = array(
'name' => 'Script Home',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.jb51.net',
) )
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>

Just execute this file, the result is as follows:
Copy code The code is as follows:

getProfile({"name":"u5e0cu4e9a"," nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.jb51.net"}})

json.php uses the json_encode function to flatten the array and then send it. On the contrary, there is a json_decode function.
So how to call it in JavaScript? It's very simple. Define a variable to get the Json sent by PHP. The Json has the characteristics of an object. We can use array.name to get the properties of the Json.
Copy code The code is as follows:










The running result is as follows:
Copy the code The code is as follows:

Script House
Gonn
xxxxxxx@163.com
http://www.jb51.net

JavaScript to PHP Pass value
json_encode.html
Copy code The code is as follows:


 
 
 
 json:From javascript To php
 
 
 

 

    

        
        


        
        


        


        


        


            
            
        


    


 
 

这里javascript扁平化需要一个插件:http://www.json.org/json2.js,通过JSON.stringify(str)将对象扁平化然后传送给php。
注:另有一个http://www.json.org/json.js,对应的是toJSONString方法。
复制代码 代码如下:

var last=obj.toJSONString(); //针对json.js
 var last=JSON.stringify(obj); //针对json2.js

json_encode.php
复制代码 代码如下:

     header('Content-Type: text/html; charset=utf-8');
     $json_string = $_POST["txt_json"];
     //echo $json_string;
     if(ini_get("magic_quotes_gpc")=="1")
     {
         $json_string=stripslashes($json_string);
     }
     $user = json_decode($json_string);

     echo var_dump($user);

     echo '



';
     echo $user->name.'
';
     echo $user->email.'
';
     echo $user->password.'
';
 ?>

Here you need to use the json_decode() function, and then call the data using the $obj-> attribute.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327745.htmlTechArticleJSON (JavaScript Object Notation) is a lightweight data exchange format. In short, both xml and json are transfer stations to facilitate the exchange of data between the client and the server...
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