My understanding and use of JSON

Source: Internet
Author: User
My understanding and use of JSON

What can I do with JSON? I really don't know. My understanding is that it is used for data caching, but it cannot be exaggerated,
You can only perform simple data caching, that is, the data volume cannot be large, but the data is frequently used and not frequently modified.
AK gave me an explanation, but I cannot find it. It should be said that it is a supplement to XML.

JSON has many language versions. The PHP version I use is multilingual, which solves the conversion problem between different Web languages.

Official station http://www.json.org/

I made an extension to the JSON version of PHP, but it is not purely closed, but encapsulated for simple use.

Code: [copy to clipboard] <? PHP
Class json_spread_server
{
VaR $ JSON;
VaR $ jsdata = "json_data.js ";
VaR $ jsfun = "json_function.js ";
VaR $ jspath = ".";
Function _ construct ($ jsonpath = './', $ jspath = '')
{
$ This-> json_spread_server ($ jsonpath );
}
Function usejsonmethod ($ method)
{
Return $ JSON-> $ method ();
}
Function json_spread_server ($ jsonpath = './', $ jspath = '')
{
Include_once ($ jsonpath. '/JSON. php ');
$ This-> JSON = new services_json ();
$ This-> jspath = ($ jspath = ""? $ This-> jspath: $ jspath ).'/';
}
Function makejsfile ($ jscontent, $ jspath = '')
{
// Annotation. When obtaining the JS file, remove the comments from the first line and the last line and decompile the PHP array.
$ Infostart = "// ----> build start". Date ('Y-m-d h: I: s'). "N ";
$ Infoend = "// ----> build end ";
$ Jspath = ""? $ This-> jspath: $ jspath .'/';

File_put_contents ($ jspath. $ this-> jsdata, $ infostart. $ jscontent. $ infoend );
}
Function buildencode ($ valuename, $ value, $ JSON = '')
{
$ JSON = is_object ($ JSON )? $ JSON: $ this-> JSON;
$ Output = $ JSON-> encode ($ value );
Return "Var {$ valuename} =". $ output. "; n ";
}
Function getphpvaluebyjsfile ($ filename = '', $ JSON = '')
{
$ Filename = ""? $ This-> jspath. $ this-> jsdata: $ filename;
If (! File_exists ($ filename) return '';
$ JSON = is_object ($ JSON )? $ JSON: New services_json ();
$ Temp = file ($ filename );
// Get rid of the last element of the array
Array_pop ($ temp );
// Get rid of the first element of the array
Array_shift ($ temp );
$ Returnarr = array ();
$ Format = '/{+ (. *) + }/';
$ Format1 = '/var (. *) = /';
For ($ I = 0; $ I <count ($ temp); $ I ++)
{
If ($ temp [$ I]! = "")
{
Preg_match_all ($ format, $ temp [$ I], $ out );
Preg_match_all ($ format1, $ temp [$ I], $ out1 );
// Get the variable array name
$ Arrname = $ rename = $ out1 [1] [0];
$ Returnarr [$ rename] = array ();
$ Arrname = array ();
$ Temparr = $ JSON-> decode ($ out [0] [0]);
If (is_object ($ temparr ))
{
Foreach (get_object_vars ($ temparr) as $ k => $ V)
{
$ Arrname [$ K] = $ V;
}
$ Returnarr [$ rename] = $ arrname;
}
}
}
Return $ returnarr;
}
}
/*
// User make file JS
$ Values = array ();
$ Values ['a'] = 'a ';
$ Values ['B'] = 'B ';
$ Values ['C'] = 'C ';
$ Makejson = new json_spread ('./','');
// Get JS file contents.
$ Jscontent = $ makejson-> buildencode ('test', $ values );
$ Jscontent. = $ makejson-> buildencode ('test2', $ values );
// Make JS file.
$ Makejson-> makejsfile ($ jscontent );
// Assace jsdata file get PHP array ();
$ Phparray = $ makejson-> getphpvaluebyjsfile ();
Print_r ($ phparray );
*/
Class json_spread_cilent extends json_spread_server
{
VaR $ htmlcode = "";
Function _ construct ($ jspath = '')
{
$ This-> json_spread_cilent ($ jspath );
}
Function json_spread_cilent ($ jspath = '')
{
$ This-> jspath = ($ jspath = ""? $ This-> jspath: $ jspath ).'/';
$ This-> htmlcode = $ this-> importjsfile ($ this-> jspath. $ this-> jsdata );
$ This-> htmlcode. = $ this-> importjsfile ($ this-> jspath. $ this-> jsfun );
}
Function makejs ($ jscommon = '')
{
$ Jscommon = str_replace ('"', '/"', $ jscommon );
Return "<script language = 'javascript '> $ jscommon </SCRIPT> N ";
}
Function importjsfile ($ jsfile)
{
Return "<script language = 'javascript 'src = '{$ jsfile}'> </SCRIPT> N ";
}
Function usejsfunecho ($ pdata, $ id, $ iswrite)
{
$ Jscommon = "echo ({$ pdata}, {$ id}, {$ iswrite });";
// $ This-> htmlcode. = $ this-> makejs ($ jscommon );
Return $ this-> makejs ($ jscommon );
}
Function setjsonloadfun ($ jsfunname)
{
$ Jscommon = "window. onload = function () {{$ jsfunname };}";
$ This-> htmlcode. = $ this-> makejs ($ jscommon );
Return $ this-> makejs ($ jscommon );
}
Function gethtmlcode ()
{
Return $ this-> htmlcode;
}
Function send ()
{
Echo $ this-> gethtmlcode ();
}

}
/*
// Use json_spread_cilent
$ Json_cilent = new json_spread_cilent ();
$ Json_cilent-> usejsfunecho ('test', 'A', true );
$ Json_cilent-> setjsonloadfun ('echo (test, 'A', 1 )');
$ Json_cilent-> send ();
*/
?>

The above code has examples for use, but I have commented out them. Test them by yourself.

Two JS files are used.

Json_function.js
Echo, the core function, wanted to encapsulate it, and the result was a little inadequate.

Code: [copy to clipboard] DEBUG = true;
Function echo (pdata, ID, iswrite)
{
VaR dvaluestr = "";
VaR dvaluearr = new array ();
Idarr = ID. Split (',');
If (Debug)
{
Alert ('function echo | id = '+ ID + "| pdata =" + pdata );
}
For (I = 0; I <idarr. length; I ++)
{
Dvaluearr [I] = pdata [idarr [I]? Pdata [idarr [I]: 'not ';

}
Dvaluestr = dvaluearr. Join (',');
If (Debug)
{
Alert ('echo: '+ dvaluestr );
}
If (iswrite)
{
Try {document. Write (dvaluestr)} catch (e) {alert ('not write ');}
} Else {
Return dvaluestr;
}

}
Function getobj (objname)
{
If (Debug)
{
Alert ('objname: '+ objname );
}
Return document. getelementbyid (objname );
}
Function setvalue (objname, value)
{
If (Debug)
{
Alert ('objname: '+ objname + "| value:" + value );
}
Getobj (objname). value = value;
}
Function getinnerhtml (objname)
{
If (Debug)
{
Alert ('objname: '+ objname );
}
Return getobj (objname). innerhtml;
}
Function setinnerhtml (objname, value)
{
If (Debug)
{
Alert ('objname: '+ objname + "| value:" + value );
}
Getobj (objname). innerhtmla = value;
}

File generated by json_data.js

Code: [copy to clipboard] // ----> build start 2006-10-30 08:05:02
VaR test = {"A": "A", "B": "B", "C": "C "};
VaR Test2 = {"A": "A", "B": "B", "C": "C "};
// ----> Build end

You must make full use of JSON.

Author: sanshi0815
Mail: sanshi0815@tom.com
 

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.