JSON ±È XML ¸üС¡¢¸ü¿ì£¬¸üÒ×½âÎö¡£
ΪʲôʹÓà JSON£¿
¶ÔÓÚ AJAX Ó¦ÓóÌÐòÀ´Ëµ£¬JSON ±È XML ¸ü¿ì¸üÒ×ʹÓãº
JSON Óï·¨¹æÔò
JSON Óï·¨ÊÇ JavaScript ¶ÔÏó±íʾ·¨Óï·¨µÄ×Ó¼¯¡£
JSON Ãû³Æ/Öµ¶Ô
JSON Êý¾ÝµÄÊéд¸ñʽÊÇ£ºÃû³Æ/Öµ¶Ô¡£
Ãû³Æ/Öµ¶Ô°üÀ¨×Ö¶ÎÃû³Æ£¨ÔÚË«ÒýºÅÖУ©£¬ºóÃæдһ¸öðºÅ£¬È»ºóÊÇÖµ£º
"name" : "wq"¡£
JSON Öµ
JSON Öµ¿ÉÒÔÊÇ£º
JSON Êý×Ö
JSON Êý×Ö¿ÉÒÔÊÇÕûÐÍ»òÕ߸¡µãÐÍ£º
{ "age":30 }
JSON ¶ÔÏó
JSON ¶ÔÏóÔÚ´óÀ¨ºÅ£¨{}£©ÖÐÊéд£º
¶ÔÏó¿ÉÒÔ°üº¬¶à¸öÃû³Æ/Öµ¶Ô£º
{ "name":"²ËÄñ½Ì³Ì" , "url":"" }
ÕâÒ»µãÒ²ÈÝÒ×Àí½â£¬ÓëÕâÌõ JavaScript Óï¾äµÈ¼Û£º
name = "²ËÄñ½Ì³Ì" url = ""
JSON Êý×é
JSON Êý×éÔÚÖÐÀ¨ºÅÖÐÊéд£º
Êý×é¿É°üº¬¶à¸ö¶ÔÏó£º
{ "sites": [ { "name":"²ËÄñ½Ì³Ì" , "url":"" }, { "name":"google" , "url":"" }, { "name":"΢²©" , "url":"" } ] }
ÔÚÉÏÃæµÄÀý×ÓÖУ¬¶ÔÏó "sites" ÊÇ°üº¬Èý¸ö¶ÔÏóµÄÊý×顣ÿ¸ö¶ÔÏó´ú±íÒ»Ìõ¹ØÓÚij¸öÍøÕ¾£¨name¡¢url£©µÄ¼Ç¼¡£
JSON ²¼¶ûÖµ
JSON ²¼¶ûÖµ¿ÉÒÔÊÇ true »òÕß false£º
{ "flag":true }
JSON null
JSON ¿ÉÒÔÉèÖà null Öµ£º
{ "runoob":null }
JSON.parse()
JSON ͨ³£ÓÃÓÚÓë·þÎñ¶Ë½»»»Êý¾Ý¡£
ÔÚ½ÓÊÕ·þÎñÆ÷Êý¾Ýʱһ°ãÊÇ×Ö·û´®¡£
ÎÒÃÇ¿ÉÒÔʹÓà JSON.parse() ·½·¨½«Êý¾Ýת»»Îª JavaScript ¶ÔÏó¡£
JSON.parse(text[, reviver])
²ÎÊý˵Ã÷£º
JSON.stringify()
JSON ͨ³£ÓÃÓÚÓë·þÎñ¶Ë½»»»Êý¾Ý¡£
ÔÚÏò·þÎñÆ÷·¢ËÍÊý¾Ýʱһ°ãÊÇ×Ö·û´®¡£
ÎÒÃÇ¿ÉÒÔʹÓà JSON.stringify() ·½·¨½« JavaScript ¶ÔÏóת»»Îª×Ö·û´®¡£
Óï·¨
JSON.stringify(value[, replacer[, space]])
²ÎÊý˵Ã÷£º
±ØÐ裬 Ò»¸öÓÐЧµÄ JSON ×Ö·û´®¡£
¿ÉÑ¡¡£ÓÃÓÚת»»½á¹ûµÄº¯Êý»òÊý×é¡£
Èç¹û replacer Ϊº¯Êý£¬Ôò JSON.stringify ½«µ÷Óøú¯Êý£¬²¢´«Èëÿ¸ö³ÉÔ±µÄ¼üºÍÖµ¡£Ê¹Ó÷µ»ØÖµ¶ø²»ÊÇÔʼֵ¡£Èç¹û´Ëº¯Êý·µ»Ø undefined£¬ÔòÅųý³ÉÔ±¡£¸ù¶ÔÏóµÄ¼üÊÇÒ»¸ö¿Õ×Ö·û´®£º""¡£
Èç¹û replacer ÊÇÒ»¸öÊý×飬Ôò½öת»»¸ÃÊý×éÖоßÓмüÖµµÄ³ÉÔ±¡£³ÉÔ±µÄת»»Ë³ÐòÓë¼üÔÚÊý×éÖеÄ˳ÐòÒ»Ñù¡£µ± value ²ÎÊýҲΪÊý×éʱ£¬½«ºöÂÔ replacer Êý×é¡£
¿ÉÑ¡£¬Îı¾Ìí¼ÓËõ½ø¡¢¿Õ¸ñºÍ»»Ðзû£¬Èç¹û space ÊÇÒ»¸öÊý×Ö£¬Ôò·µ»ØÖµÎı¾ÔÚÿ¸ö¼¶±ðËõ½øÖ¸¶¨ÊýÄ¿µÄ¿Õ¸ñ£¬Èç¹û space ´óÓÚ 10£¬ÔòÎı¾Ëõ½ø 10 ¸ö¿Õ¸ñ¡£space ÓпÉÒÔʹÓ÷ÇÊý×Ö£¬È磺\t¡£
×¢Ò⣺
JSONÊÇÊý¾Ý½»»¥ÖУ¬×î³£ÓõÄÒ»ÖÖÊý¾Ý¸ñʽ¡£
*
* ÓÉÓÚ¸÷ÖÖÓïÑÔµÄÓï·¨¶¼²»Ïàͬ£¬ÔÚ´«µÝÊý¾Ýʱ£¬¿ÉÒÔ½«×Ô¼ºÓïÑÔÖÐÊý×é¡¢¶ÔÏóµÈת»¯ÎªJSON×Ö·û´®¡£´«µÝÖ®ºó£¬¿ÉÒÔ½«JSON
* ×Ö·û´®£¬ÔÙ½âÎöΪJSON¶ÔÏó£»
*
* JSON¶ÔÏóµÄÓû§ÓëJSÖеĶÔÏó»ù±¾Ïàͬ£¬Î¨Ò»Çø±ðµÄÊÇ£ºJSONÖеļü±ØÐëÊÇ×Ö·û´®£»
WEB´æ´¢
HTML5ÐÂÔöweb´æ´¢·½Ê½£¬Ö÷ÒªÓÐÁ½ÖÖ£º
¿Í»§¶Ë´æ´¢Êý¾ÝµÄÁ½¸ö¶ÔÏóΪ£º
localStorage - ûÓÐʱ¼äÏÞÖƵÄÊý¾Ý´æ´¢
sessionStorage - Õë¶ÔÒ»¸ö session µÄÊý¾Ý´æ´¢
Á½¸ö¶ÔÏóÔÚʹÓ÷½Ê½Ã»ÓÐÈκÎÇø±ð£¬Î¨Ò»µÄÇø±ðÊÇ´æ´¢µÄÊý¾ÝµÄÓÐЧʱ¼ä£»
¢ÙlocalStorage £º³ý·ÇÊÖ¶¯É¾³ý£¬·ñÔòÊý¾Ý½«Ò»Ö±±£´æÔÚ±¾µØÎļþ£»
¢ÚsessionStorage£ºµ±ä¯ÀÀÆ÷¹Ø±Õʱ£¬sessionStorage¾Í±»Çå¿Õ£»
[StorageµÄÊý¾Ý´æ´¢]
1¡¢Storage ¿ÉÒÔÏñÆÕͨ¶ÔÏóÒ»Ñù£¬Ê¹ÓÃ.×·¼Ó»òÕ߶ÁÈ¡×îеÄÊý¾Ý¡£
eg£ºlocalStorage.username = "zhangsan";
2¡¢³£Óõĺ¯Êý£º
±£´æÊý¾Ý£ºlocalStorage.setItem(key,value); ¶ÁÈ¡Êý¾Ý£ºlocalStorage.getItem(key); ɾ³ýµ¥¸öÊý¾Ý£ºlocalStorage.removeItem(key); ɾ³ýËùÓÐÊý¾Ý£ºlocalStorage.clear(); µÃµ½Ä³¸öË÷ÒýµÄkey£ºlocalStorage.key(index);ÔÚHTML5ÖУ¬Ð¼ÓÈëÁËÒ»¸ölocalStorageÌØÐÔ£¬Õâ¸öÌØÐÔÖ÷ÒªÊÇÓÃÀ´×÷Ϊ±¾µØ´æ´¢À´Ê¹Óõģ¬½â¾öÁËcookie´æ´¢¿Õ¼ä²»×ãµÄÎÊÌâ(cookieÖÐÿÌõcookieµÄ´æ´¢¿Õ¼äΪ4k)£¬localStorageÖÐÒ»°ãä¯ÀÀÆ÷Ö§³ÖµÄÊÇ5M´óС£¬Õâ¸öÔÚ²»Í¬µÄä¯ÀÀÆ÷ÖÐlocalStorage»áÓÐËù²»Í¬¡£
localStorageµÄÓÅÊÆ
1¡¢localStorageÍØÕ¹ÁËcookieµÄ4KÏÞÖÆ
2¡¢localStorage»á¿ÉÒÔ½«µÚÒ»´ÎÇëÇóµÄÊý¾ÝÖ±½Ó´æ´¢µ½±¾µØ£¬Õâ¸öÏ൱ÓÚÒ»¸ö5M´óСµÄÕë¶ÔÓÚÇ°¶ËÒ³ÃæµÄÊý¾Ý¿â£¬Ïà±ÈÓÚcookie¿ÉÒÔ½ÚÔ¼´ø¿í£¬µ«ÊÇÕâ¸öÈ´ÊÇÖ»ÓÐÔڸ߰汾µÄä¯ÀÀÆ÷ÖвÅÖ§³ÖµÄ
localStorageµÄ¾ÖÏÞ
1¡¢ä¯ÀÀÆ÷µÄ´óС²»Í³Ò»£¬²¢ÇÒÔÚIE8ÒÔÉϵÄIE°æ±¾²ÅÖ§³ÖlocalStorageÕâ¸öÊôÐÔ
2¡¢Ä¿Ç°ËùÓеÄä¯ÀÀÆ÷Öж¼»á°ÑlocalStorageµÄÖµÀàÐÍÏÞ¶¨ÎªstringÀàÐÍ£¬Õâ¸öÔÚ¶ÔÎÒÃÇÈÕ³£±È½Ï³£¼ûµÄJSON¶ÔÏóÀàÐÍÐèҪһЩת»»
3¡¢localStorageÔÚä¯ÀÀÆ÷µÄÒþ˽ģʽÏÂÃæÊDz»¿É¶ÁÈ¡µÄ
4¡¢localStorage±¾ÖÊÉÏÊǶÔ×Ö·û´®µÄ¶ÁÈ¡£¬Èç¹û´æ´¢ÄÚÈݶàµÄ»°»áÏûºÄÄÚ´æ¿Õ¼ä£¬»áµ¼ÖÂÒ³Ãæ±ä¿¨
5¡¢localStorage²»Äܱ»ÅÀ³æץȡµ½
localStorageÓësessionStorageµÄΨһһµãÇø±ð¾ÍÊÇlocalStorageÊôÓÚÓÀ¾ÃÐÔ´æ´¢£¬¶øsessionStorageÊôÓÚµ±»á»°½áÊøµÄʱºò£¬sessionStorageÖеļüÖµ¶Ô»á±»Çå¿Õ
ÕâÀïÎÒÃÇÒÔlocalStorageÀ´·ÖÎö
Èý¡¢localStorageµÄʹÓÃ
¡¡