JSON 之我的理解跟使用

JSON 之我的理解跟使用

JSON 这个东西,到底能干什么,我还真不很清楚,我的理解就是,做数据缓存用的,但是不能做很夸张的数据缓存,
只能做简单的数据缓存,也就是数据量不能很大,但是使用比较频繁,修改不频繁这样的数据。
ak给我过一个他的解释,但是我找不到了,应该说是一个对xml的补充什么的。

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;$i<count($temp);$i++)
        {
            if(
$temp[$i]!="")
            {
                
preg_match_all($format,$temp[$i],$out);
                
preg_match_all($format1,$temp[$i],$out1);
                
//取得变量数组名称
                
$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.οnlοad=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 本想好好封装下,结果有点力不从心

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;
}
JSON_data.js  生成的文件

//----> 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 的使用,还要各位自己去发挥了。

作者:sanshi0815
mail : sanshi0815@tom.com
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值