|
|

JSON 有很多语言版本,我使用的php的版本的,多语言的,解决了,不同web语言间的转换问题。
官方站 http://www.json.org/
我对php版本的JSON 做了一个扩展,但是封闭的不纯粹,只是对简单的使用做了封装。
<?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=”)
{
//注释,在取得js文件的时候,把首行跟最后一行的注释去掉后,反编译取得php数组
$infoStart = “//—-> build start “.date(’Y-m-d H:i:s’).”\n”;
$infoEnd = “//—-> build end “;
$jsPath = $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 = $fileName==”"? $this->jsPath.$this->jsData fileName;
if(!file_exists($fileName)) return ”;
$json = is_object($json) ? $json : new Services_JSON();
$temp = file($fileName);
//驱除数组最后一个元素
array_pop($temp);
//驱除数组第一个元素
array_shift($temp);
$returnArr = array();
$format = ‘/\{+(.*)+\}/’;
$format1 = ‘/var (.*) =/’;
for($i=0;$idecode($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();
*/
?>
复制代码上面的代码,都有使用的例子,但是我给注释掉了,自己测试下吧
用到了2个js文件
JSON_function.js
核心函数就是 echo 本想好好封装下,结果有点力不从心
CODE: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 = pdata[idArr]?pdata[idArr]:’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;
}
JSON_data.js 生成的文件
CODE://—-> 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
关于json 的使用,还要各位自己去发挥了。 |
|