How to Use json in php and javascript
1. Introduction to JSON
JSON (javascript Object Notation) is a lightweight data exchange format that is easy to read and write.
It is also easy to parse and generate. JSON in a language-independent text format, but it also uses a similar
In the C family coding habits (C ++, C, C #, java, javascript, Perl, Python), these features make JSON an ideal data exchange language. For JSON interpretation, we can visit the official website on our own.
Or you can search for it on Wikipedia.
JSON encoding in php
It is easy to generate a JSON string in php. JSON can be converted to a string directly using the json_encode () function.
The function is prototype string json_encode (mixed $ value );
This function can wake up transcoding for any data except the rescoure type.
Iii. parse JSON using javascript
Js parses JSON in two ways. One is to directly use the eval () function. This method is the fastest. Since the eval () method can execute any js Code, security problems may occur when the data source is unreliable.
For example, in the following example, the page will be redirected.
A section of JSON data that causes page redirection using eval () Interpretation<Script type = "text/javascript" src = ".. /jquery-1.7.min.js "> </script> <script type =" text/javascript "> // jquery get JSON data through AJAX $ (document ). ready (function () {var dangerJson = '{message :( function () {window. location = \ 'HTTP: // blog.csdn.net/morewindows}';}) ()} '; // eval (dangerJson); // redirects var jsonArray = JSON. parse (dangerJson); // an error is returned-invalid character}); </script>A section of JSON data that causes page redirection using eval () Interpretation
The second method can prevent Insecure code from appearing-JSON supported by the browser native. the parse (str) method reads JSON data. This method uses the interpreter to verify that the read code is true JSON and the Code provides better security, however, the read speed is slower than that of eval ().
4. JSON example
The following uses an example to explain how the data is encoded as a JSON string, and how the JSON string is interpreted and used in js.
Array ("title" => "PHP accessing MySql database Beginners", "link" => "http://blog.csdn.net/morewindows/article/details/7102362 "), "002" => array ("title" => "PHP accessing MySql database intermediate Smarty technology", "link" => "http://blog.csdn.net/morewindows/article/details/7094642 "), "003" => array ("title" => "PHP advanced AJAX technology for MySql database access", "link" => "http://blog.csdn.net/morewindows/article/details/7086524 "),); $ tpl_article_json = json_encode ($ tpl_article_array); $ Tpl = new Smarty (); $ tpl-> assign ("article_array", $ tpl_article_array); $ tpl-> assign ("article_json", $ tpl_article_json ); $ tpl-> display ("json1.html");?>
Json1.html
Use json<Script type = "text/javascript" src = ".. /jquery-1.7.min.js "> </script> <script type =" text/javascript "src =" json2.js "> </script> <script type =" text/javascript "> $ (document ). ready (function () {var g_jsonstr = JSON. parse ('{$ article_json}'); // use JSON. parse () parses the JSON string $ ("div "). mouseenter (function () {// mouseenter mouseovervar thisId = $ (this ). attr ("id"); var jsonid = thisId. substring (thisId. lastIndexOf ("_") + 1, thisId. length); $ ("# article_link" ).css ("position", "absolute"); $ ("# article_link" ).css ("left", "20px "); $ ("# article_link" ).css ("top", $ (this ). offset (). top + $ (this ). height (); $ ("# article_link" ).html ("link address" + g_jsonstr [jsonid] ['link']); $ ("# article_link "). slideDown ("fast" has been broken (this).css ("background-color", "red") ;}); $ ("div "). mouseleave (function () {// mouseleave mouseout $ ("# article_link" (.hide();$((this..css ("background-color", "yellow") ;}); </script>{Foreach $ article_array as $ key = >$ value} {$ value ['title']} {/foreach}