<script type="text/javascript">
function getProfile(str) {
var arr = str;
document.getElementById('nick').innerHTML = arr.nick;
}
</script>
<body><div></div></body>
<script type="text/javascript" src="http://www.openphp.cn/demo/profile.php"></script>
±»µ÷Îļþprofile.php
´úÂëÈçÏÂ:
<?php
$arr = array(
'name' => '½Å±¾Ö®¼Ò',
'nick' => 'Éî¿Õ',
'contact' => array(
'email' => 'shenkong at qq dot com',
'website' => 'http://www.jb51.net',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
ºÜÏÔÈ»£¬µ±index.htmlµ÷ÓÃprofile.phpʱ£¬JSON×Ö·û´®Éú³É£¬²¢×÷Ϊ²ÎÊý´«ÈëgetProfile£¬È»ºó½«êdzƲåÈëµ½divÖУ¬ÕâÑùÒ»´Î¿çÓòÊý¾Ý½»»¥¾ÍÍê³ÉÁË£¬ÊDz»ÊÇÌرð¼òµ¥¡£¼ÈÈ»JSONÕâô¼òµ¥Ò×ÓöøÇÒºÃÓ㬻¹µÈʲôÄØ£¿^_^
·þÎñÆ÷¶ËµÄ JSON
½« JSON ·¢¸ø·þÎñÆ÷
½« JSON ·¢¸ø·þÎñÆ÷²¢²»ÄÑ£¬µ«È´ÖÁ¹ØÖØÒª£¬¶øÇÒ»¹ÓÐһЩÖØÒªµÄÑ¡ÔñÒª×ö¡£µ«ÊÇ£¬Ò»µ©¾ö¶¨Ê¹Óà JSON£¬ËùÒª×öµÄÕâЩѡÔñ¾Í»áÊ®·Ö¼òµ¥¶øÇÒÊýÁ¿ÓÐÏÞ£¬ËùÒÔÄúÐèÒª¿¼ÂǺ͹Ø×¢µÄÊÂÇé²»¶à¡£ÖØÒªµÄÊÇÄܹ»½« JSON ×Ö·û´®·¢Ë͸ø·þÎñÆ÷£¬¶øÇÒ×îºÃÄÜ×öµ½¾¡¿ìºÍ¾¡¿ÉÄܼòµ¥¡£
ͨ¹ý GET ÒÔÃû³Æ/Öµ¶Ô·¢ËÍ JSON
½« JSON Êý¾Ý·¢¸ø·þÎñÆ÷µÄ×î¼òµ¥·½·¨Êǽ«Æäת»»³ÉÎı¾£¬È»ºóÒÔÃû³Æ/Öµ¶ÔµÄÖµµÄ·½Ê½½øÐз¢ËÍ¡£ÇëÎñ±Ø×¢Ò⣬JSON ¸ñʽµÄÊý¾ÝÊÇÏ൱³¤µÄÒ»¸ö¶ÔÏ󣬿´ÆðÀ´¿ÉÄÜ»áÈçÇåµ¥ 1 Ëùʾ£º
Çåµ¥ 1. JSON ¸ñʽµÄ¼òµ¥ JavaScript ¶ÔÏó
´úÂëÈçÏÂ:
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin",
"email": "brett@newInstance.com" }, { "firstName": "Jason", "lastName":"Hunter",
"email": "jason@servlets.com" }, { "firstName": "Elliotte", "lastName":"Harold",
"email": "elharo@macfaq.com" } ], "authors": [ { "firstName": "Isaac",
"lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad",
"lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank",
"lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] }
Èç¹ûÒªÒÔÃû³Æ/Öµ¶Ô½«Æä·¢Ë͵½·þÎñÆ÷¶Ë£¬Ó¦¸ÃÈçÏÂËùʾ£º
´úÂëÈçÏÂ:
var url = "organizePeople.php?people=" + people.toJSONString();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
Õâ¿´ÆðÀ´²»´í£¬µ«È´´æÔÚÒ»¸öÎÊÌ⣺ÔÚ JSON Êý¾ÝÖлáÓпոñºÍ¸÷ÖÖ×Ö·û£¬Web ä¯ÀÀÆ÷ÍùÍùÒª³¢ÊÔ¶ÔÆä¼ÌÐø±àÒ롣Ҫȷ±£ÕâЩ×Ö·û²»»áÔÚ·þÎñÆ÷ÉÏ£¨»òÕßÔÚ½«Êý¾Ý·¢Ë͸ø·þÎñÆ÷µÄ¹ý³ÌÖУ©ÒýÆð»ìÂÒ£¬ÐèÒªÔÚ JavaScript escape() º¯ÊýÖÐ×öÈçÏÂÌí¼Ó£º
´úÂëÈçÏÂ:
var url = "organizePeople.php?people=" + escape(people.toJSONString());
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
¸Ãº¯Êý¿ÉÒÔ´¦Àí¿Õ¸ñ¡¢Ð±ÏߺÍÆäËûÈκοÉÄÜÓ°Ïìä¯ÀÀÆ÷µÄÄÚÈÝ£¬²¢½«ËüÃÇת»»³É Web ¿ÉÓÃ×Ö·û£¨±ÈÈ磬¿Õ¸ñ»á±»×ª»»³É %20£¬ä¯ÀÀÆ÷²¢²»»á½«ÆäÊÓΪ¿Õ¸ñ´¦Àí£¬¶øÊDz»×ö¸ü¸Ä£¬½«ÆäÖ±½Ó´«µÝµ½·þÎñÆ÷£©¡£Ö®ºó£¬·þÎñÆ÷»á£¨Í¨³£×Ô¶¯£©ÔÙ°ÑËüÃÇת»»»ØËüÃÇ´«ÊäºóµÄ±¾À´ ¡°ÃæÄ¿¡±¡£
ÕâÖÖ×ö·¨µÄȱµãÓÐÁ½¸ö£º
ÔÚʹÓà GET ÇëÇó·¢ËÍ´ó¿éÊý¾Ýʱ£¬¶Ô URL ×Ö·û´®Ó㤶ÈÏÞÖÆ¡£ËäÈ»Õâ¸öÏÞÖÆºÜ¿í·º£¬µ«¶ÔÏóµÄ JSON ×Ö·û´®±íʾµÄ³¤¶È¿ÉÄܳ¬³öÄúµÄÏëÏó£¬ÓÈÆäÊÇÔÚʹÓü«Æ临ÔӵĶÔÏóʱ¸üÊÇÈç´Ë¡£
ÔÚ¿çÍøÂçÒÔ´¿Îı¾·¢ËÍËùÓÐÊý¾ÝµÄʱºò£¬·¢ËÍÊý¾ÝÃæÁٵIJ»°²È«ÐÔ³¬³öÁËÄúµÄ´¦ÀíÄÜÁ¦¡£
¼òÑÔÖ®£¬ÒÔÉÏÊÇ GET ÇëÇóµÄÁ½¸öÏÞÖÆ£¬¶ø²»ÊǼòµ¥µÄÁ½¸öÓë JSON Êý¾ÝÏà¹ØµÄÊÂÇé¡£ÔÚÏëÒª·¢ËÍÓû§ÃûºÍÐÕÖ®ÍâµÄ¸ü¶àÄÚÈÝ£¬±ÈÈç±íµ¥ÖеÄÑ¡Ôñʱ£¬¶þÕß¿ÉÄÜ»áÐèÒª¶à¼Ó×¢Òâ¡£ÈôÒª´¦ÀíÈκλúÃÜ»ò¼«³¤µÄÄÚÈÝ£¬¿ÉÒÔʹÓà POST ÇëÇó¡£
ÀûÓà POST ÇëÇó·¢ËÍ JSON Êý¾Ý
µ±¾ö¶¨Ê¹Óà POST ÇëÇó½« JSON Êý¾Ý·¢Ë͸ø·þÎñÆ÷ʱ£¬²¢²»ÐèÒª¶Ô´úÂë½øÐдóÁ¿¸ü¸Ä£¬ÈçÏÂËùʾ£º
´úÂëÈçÏÂ:
var url = "organizePeople.php?timeStamp=" + new Date().getTime();
request.open("POST", url, true);
request.onreadystatechange = updatePage;
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(people.toJSONString());
ÕâЩ´úÂëÖеĴ󲿷֣¬Äú¶¼ÔÚ ¡° ÕÆÎÕ Ajax£¬µÚ 3 ²¿·Ö£ºAjax Öеĸ߼¶ÇëÇóºÍÏìÓ¦¡± Öмû¹ý£¬Ó¦¸Ã±È½ÏÊìϤ£¬µÚ 3 ²¿·ÖÖصã½éÉÜÁËÈçºÎ·¢ËÍ POST ÇëÇó¡£ÇëÇóʹÓà POST ¶ø·Ç GET ´ò¿ª£¬¶øÇÒ Content-Type Í·±»ÉèÖÃΪÈ÷þÎñÆ÷Ô¤ÖªËüÄܵõ½ºÎÖÖÊý¾Ý¡£ÔÚÕâÖÖÇé¿öÏ£¬¼´Îª application/x-www-form-urlencoded£¬ËüÈ÷þÎñÆ÷ÖªµÀÏÖÔÚ·¢Ë͵ÄÊÇÎı¾£¬ÕýÈçËü´Ó³£¹æµÄ HTML ±íµ¥Öеõ½µÄÒ»Ñù¡£
Áí Ò»¸ö¼òµ¥ÌáʾÊÇ URL µÄĩβ׷¼ÓÁËʱ¼ä¡£Õâ¾ÍÈ·±£ÁËÇëÇó²»»áÔÚËüµÚÒ»´Î±»·¢Ëͺ󼴻º´æ£¬¶øÊÇ»áÔÚ´Ë·½·¨Ã¿´Î±»µ÷ÓúóÖØд´½¨ºÍÖØ·¢£»´Ë URL »áÓÉÓÚʱ¼ä´ÁµÄ²»Í¬¶øÉÔ΢ÓÐЩ²»Í¬¡£ÕâÖÖ¼¼Çɳ£±»ÓÃÓÚÈ·±£µ½½Å±¾µÄ POST ÿ´Î¶¼»áʵ¼ÊÉú³ÉÐÂÇëÇóÇÒ Web ·þÎñÆ÷²»»á³¢ÊÔ»º´æÀ´×Ô·þÎñÆ÷µÄÏìÓ¦¡£
JSON ¾ÍÖ»ÊÇÎı¾
²»¹ÜʹÓà GET »¹ÊÇ POST£¬¹Ø¼üÖ®´¦ÔÚÓÚ JSON ¾ÍÖ»ÊÇÎı¾¡£ÓÉÓÚ²»ÐèÒªÌØÊâ±àÂë¶øÇÒÿ¸ö·þÎñÆ÷¶Ë½Å±¾¶¼ÄÜ´¦ÀíÎı¾Êý¾Ý£¬ËùÒÔ¿ÉÒÔÇáËÉÀûÓà JSON ²¢½«ÆäÓ¦Óõ½·þÎñÆ÷¡£¼ÙÈç JSON ÊǶþ½øÖƸñʽµÄ»òÊÇһЩ¹ÖÒìµÄÎı¾±àÂ룬Çé¿ö¾Í²»Õâô¼òµ¥ÁË£»ÐҺà JSON Ö»Êdz£¹æµÄÎı¾Êý¾Ý£¨ÕýÈç½Å±¾ÄÜ´Ó±íµ¥Ìá½»ÖÐËù½ÓÊÕµ½µÄÊý¾Ý£¬ÔÚ POST ¶ÎºÍ Content-Type Í·ÖÐÒà¿ÉÒÔ¿´³ö£©£¬ËùÒÔÔÚ½«Êý¾Ý·¢Ë͵½·þÎñÆ÷ʱÎÞÐèÌ«·ÑÐÄ¡£
ÔÚ·þÎñÆ÷ÉϽâÊÍ JSON
Ò» µ©Äú±àдÍê¿Í»§¶Ë JavaScript ´úÂë¡¢ÔÊÐíÓû§Óë Web ±íµ¥ºÍ Web Ò³µÄ½»»¥¡¢ÊÕ¼¯·¢Ë͸ø·þÎñÆ÷¶Ë³ÌÐòÒÔ×ö´¦ÀíËùÐèµÄÐÅÏ¢£¬´Ëʱ£¬·þÎñÆ÷¾Í³ÉΪÁËÓ¦ÓóÌÐò£¨Èç¹ûµ÷ÓÃÁËÒ첽ʹÓõķþÎñÆ÷¶Ë³ÌÐò£¬Ôò¿ÉÄÜÊÇÎÒÃÇÈÏΪµÄËùνµÄ ¡°Ajax Ó¦ÓóÌÐò¡±£©ÖеÄÖ÷½Ç¡£ÔÚ´Ëʱ£¬ÄúÔÚ¿Í»§¶ËËù×öµÄÑ¡Ôñ£¨±ÈÈçʹÓà JavaScript ¶ÔÏó£¬È»ºó½«Æäת»»³É JSON ×Ö·û´®£©±ØÐëÒªÓë·þÎñÆ÷¶ËµÄÑ¡ÔñÏàÆ¥Å䣬±ÈÈçʹÓÃÄĸö API ½âÂë JSON Êý¾Ý¡£
´¦Àí JSON µÄÁ½²½Öè
²»¹ÜÔÚ·þÎñÆ÷¶ËʹÓúÎÖÖÓïÑÔ£¬ÔÚ·þÎñÆ÷¶Ë´¦Àí JSON »ù±¾ÉϾÍÐèÒªÁ½¸ö²½Öè¡£
Õë¶Ô±àд·þÎñÆ÷¶Ë³ÌÐòËùÓõÄÓïÑÔ£¬ÕÒµ½ÏàÓ¦µÄ JSON ½âÎöÆ÷/¹¤¾ßÏä/°ïÖúÆ÷ API¡£
ʹÓà JSON ½âÎöÆ÷/¹¤¾ßÏä/°ïÖúÆ÷ API È¡µÃÀ´×Ô¿Í»§»úµÄÇëÇóÊý¾Ý²¢½«Êý¾Ýת±ä³É½Å±¾ÄÜÀí½âµÄ¶«Î÷¡£
ÒÔÉϲ¶à¾ÍÊÇÄ¿Ç°ËùÓ¦Á˽âµÄ´óÖÂÄÚÈÝÁË¡£½ÓÏÂÀ´£¬ÎÒÃǶÔÿ¸ö²½Öè½øÐнÏΪÏêϸµÄ½éÉÜ¡£
Ñ°ÕÒ JSON ½âÎöÆ÷
Ñ°ÕÒ JSON ½âÎöÆ÷»ò¹¤¾ßÏä×îºÃµÄ×ÊÔ´ÊÇ JSON Õ¾µã£¨ÓйØÁ´½Ó£¬Çë²ÎÔÄ ²Î¿¼×ÊÁÏ£©¡£ ÔÚÕâÀ³ýÁË¿ÉÒÔÁ˽â´Ë¸ñʽ±¾ÉíµÄ·½·½ÃæÃæÖ®Í⣬»¹¿ÉÒÔͨ¹ý¸÷ÖÖÁ´½ÓÕÒµ½ JSON µÄ¸÷ÖÖ¹¤¾ßºÍ½âÎöÆ÷£¬´Ó ASP µ½ Erlang£¬µ½ Pike£¬ÔÙµ½ Ruby£¬Ó¦Óо¡ÓС£ÄúÖ»ÐèÕë¶Ô×Ô¼º±àд½Å±¾ËùÓõÄÓïÑÔÏÂÔØÏàÓ¦µÄ¹¤¾ßÏä¼´¿É¡£ÎªÁËÈ÷þÎñÆ÷¶Ë½Å±¾ºÍ³ÌÐòÄܹ»Ê¹Óô˹¤¾ßÏ䣬¿ÉÒÔ¸ù¾ÝÇé¿ö¶ÔÆä½øÐÐÑ¡Ôñ¡¢À© Õ¹»ò°²×°£¨Èç¹ûÔÚ·þÎñÆ÷¶ËʹÓõÄÊÇ C#¡¢PHP »ò Lisp£¬Ôò¿É±äÐÔ¸ü´ó£©¡£
ÀýÈ磬Èç¹ûʹÓõÄÊÇ PHP£¬¿ÉÒÔ¼òµ¥½«ÆäÉý¼¶ÖÁ PHP 5.2 ²¢ÓÃËüÍê³É²Ù×÷£»ÔÚ PHP Õâ¸ö×îа汾ĬÈÏ°üº¬ÁË JSON À©Õ¹¡£Êµ¼ÊÉÏ£¬ÄÇÒ²ÊÇÔÚʹÓà PHP ʱ´¦Àí JSON µÄ×îºÃ·½·¨¡£Èç¹ûʹÓõÄÊÇ Java servlet£¬json.org É쵀 org.json °üÏÔÈ»¾ÍÊǸö²»´íµÄÑ¡Ôñ¡£ÔÚÕâÖÖÇé¿öÏ£¬¿ÉÒÔ´Ó JSON Web Õ¾µãÏÂÔØ json.zip ²¢½«ÆäÖаüº¬µÄÔ´ÎļþÌí¼Óµ½ÏîÄ¿¹¹½¨Ä¿Â¼¡£±àÒëÍêÕâЩÎļþºó£¬Ò»Çо;ÍÐ÷ÁË¡£¶ÔÓÚËùÖ§³ÖµÄÆäËûÓïÑÔ£¬Í¬Ñù¿ÉÒÔʹÓÃÏàͬµÄ²½Ö裻ʹÓúÎÖÖÓïÑÔÈ¡¾öÓÚÄú¶Ô¸ÃÓï ÑԵľ«Í¨³Ì¶È£¬×îºÃʹÓÃÄúËùÊìϤµÄÓïÑÔ¡£
ʹÓà JSON ½âÎöÆ÷
Ò»µ©»ñµÃÁ˳ÌÐò¿ÉÓõÄ×ÊÔ´£¬Ê£ÏµÄʾÍÊÇÕÒµ½ºÏÊʵķ½·¨½øÐе÷ÓᣱÈÈ磬¼ÙÉèΪ PHP ʹÓõÄÊÇ JSON-PHP Ä£°å£º
´úÂëÈçÏÂ:
¡¡
¡¡