JSON advanced Article 1 use JSON in PHP and JavaScript

Source: Internet
Author: User
Tags php mysql
1. Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. For more information about JSON, visit the JSON official website. We recommend that you study it carefully. In addition, the introduction of JSON in Wikipedia is also very detailed. You can visit it.

Ii. JSON encoding in PHP

It is very easy to generate JSON strings in PHP.Json_encode ()The function can convert PHP data into a JSON string. The original form of this function is as follows:

StringJson_encode(Mixed$ Value)

This function can transcode any data except the resource type.

Iii. parse JSON using JavaScript

There are two methods: one is to directly use the eval () function. This method is the fastest. However, because the eval method can execute arbitrary JavaScript code, security issues may occur when the data source is unreliable. For example, the following example causes page redirection:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

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 a parser to verify whether the read code is actually JSON code, which provides better security. However, because the read speed is simulated, It is slower than eval.

Iv. JSON instance

. The program also references the smarty, jquery, and JSON library files.

1. json1.php

<? PHP // By morewindows (http://blog.csdn.net/MoreWindows) require_once ('.. /.. /smarty_libs/smarty. class. PHP '); $ tpl_article_array = array ("001" => array ("title" => "php MySQL database beginner", "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 access MySQL Database Advanced Ajax technology, "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");?>

2.json1.html

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

The running result is as follows (win7 + ie9.0 ):

When the mouse goes through three headers, The mouseenter event is triggered to display the prompt statement.

The next article "JSON advanced Article 2 transmitting JSON Data Using ajax" describes how to use Ajax dynamic requests to obtain JSON data and generate titles and prompt statements.

 

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/7197971

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.