Home  >  Article  >  Backend Development  >  everest ultimate edition PHP6 preparatory course JSON example code

everest ultimate edition PHP6 preparatory course JSON example code

WBOY
WBOYOriginal
2016-07-29 08:38:391080browse

It is a subset based on JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999
JSON mainly uses pairs of {} to wrap each object (object), and pairs of [] to wrap each array( array),
Use pairs of "" to wrap each string, use commas to separate each variable, and the data types include string, number, array, object
The following simple JSON format describes an object json that has an Member variable, this member variable contains three objects
Copy content to the clipboard code:
var json = {
'query' : [
{'id':'1','type':'a',' title':'PHP 5.2.0's new function JSON decoder & encoder'},
{'id':'2','type':'b','title':'JSON full name JavaScript Object Notation'},
{'array': ['A', 'B','C', 'D', 'E']}
]
};
In this way, we can get an Object called json, and this json Object contains An independent member query
And query contains an Array. This Array contains three Objects. The first two Objects contain three members
id, type, title, and the last Object array contains an array. This explanation is also clear. Bar?
But how to use it?
Very simple
alert('I have ' +json.query.length + ' object.');
//alert I have 3 object.
alert('type='+json.query[1].type+'rntitle '+json.query[1].title);
//alert type=b title=JSON full name JavaScript Object Notation
alert('array index 3='+json.query[2].array[3]);
//alert array index 3=D
This makes it easier to operate data. There is no need to deal with complex DOM. The required data can be easily obtained
For example, the above example json.query[ i ].title can be obtained in this way The value contained in the i-th title
PHP is developing very rapidly. When the programming world still has little knowledge of JSON or has no idea what JSON is at all
PHP has been incorporated into the core in the latest version 5.2.0, and the default state Yes, compared to other Script languages ​​
PHP is leading the way. In version 5.2.0, it has implemented two functions json_decode() and json_encode() for JSON
The former restores JSON format strings to PHP native Array
The latter compiles PHP native array into a string in JSON format
However, since Javascript supports Unicode, if you use non-Ascii characters when accessing the database, such as Chinese, Japanese, and Korean
You need to change the characters Convert the encoding to UTF8, otherwise the string after json_encode() will be garbled
================================== = =======================
After a brief introduction to JOSN in the previous article
This article will implement how to use JOSN
The following examples require the use of MySQL4 . Version 1 or above
The whole coding process uses utf8
Follow the data format of the previous article. There are three fields in the table: id, type, title
The specifications of the data table are as follows
Copy the content to the clipboard code:
CREATE TABLE `news` (
` id` int(10) unsigned NOT NULL auto_increment,
`type` varchar(255) NOT NULL default '',
`title` varchar(64) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE =MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
Copy content to clipboard code:
//Establish connection
$conn = mysqli_connect("localhost", 'root', '')or die( 'Unable to connect to the database');
//Select the database
mysqli_select_db($conn,'mydata') or die('Can't select the database');
//Set the connection encoding rules, don't know how to use google Find
mysqli_query($conn,'SET NAMES 'utf8'');
//Get data
$results = mysqli_query($conn,'SELECT id,type,title FROM news');
//Josn string
$ json = '';
//Because it is an example, you can control the loop by yourself
$i=0;
while($row = mysqli_fetch_assoc($results))
{
$i++;
$json .= json_encode($row );
//There are only three pieces of data in the data table, so there is no need to add "," at the end of the third piece of data. Remember, there is no need to add "," to the last piece of data
if ($i<3)
{
$json .= ",";
}
}
//Pack the data into the array
$json = '{"query":[ '.$json.']}';?>
< ;!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


Json Example< /title> <br><meta http-equiv="Content-Type" c /> <br><meta http-equiv="Pragma" c /> <br><meta http-equiv="Expires" c /> <br><meta http-equiv="Cache-Control" c /> <br><meta name="generator" c /> <br></head> <br><body> <br><script type= "text/ javascript"> <br>var json = <?php echo $json?>; <br>alert('I have ' +json.query.length + ' object.'); <br>alert('type='+ json.query [1].type+'rntitle'+json.query[1].title); <br>//Used in the previous introduction<br></script> <br>Restore Json<br> <br><?php <br> //Decode the string <br>$s_JSON_Decoded = json_decode($json,true); <br>//Retrieve data <br>foreach ($s_JSON_Decoded as $row) <br>{ <br>foreach ($row as $rowa) <br>{ <br>echo $rowa ['title ']."<br>"; <br>} <br>} <br>?> <br></body> <br></html> <br>After a simple drill<br>I believe everyone has a deeper understanding of JSON. Understanding <br> Of course, the application of JSON is not just as simple as the example <br> If you are interested, let’s study together </p> <p> The above introduces the JSON example code of everest ultimate edition PHP6 preparatory class, including the content of everest ultimate edition. I hope it will be helpful to friends who are interested in PHP tutorials. </p> <p> </p></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>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</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="wupload swfupload multiple file upload implementation code" href="http://m.php.cn/faq/321922.html">wupload swfupload multiple file upload implementation code</a></span><span>Next article:<a class="dBlack" title="wupload swfupload multiple file upload implementation code" href="http://m.php.cn/faq/321924.html">wupload swfupload multiple file upload implementation code</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai" >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>