ajax中,使用Json代替xml加快速度
来源:互联网 发布:ios电视直播软件2017 编辑:程序博客网 时间:2024/04/29 16:09
<?php
/*----------------------------------------------------------------------
PHP JSON Class
==============
The PHP JSON Class can be used to encode a php array or object into
Java Script Object Notation, without the need for an additional PHP
Extension.
Normal usage is as follows:
$json = new json;
$encoded = $json->encode($var);
echo $encoded;
Version 0.5
Copyright Jack Sleight - www.reallyshiny.com
This script is licensed under the:
Creative Commons Attribution-ShareAlike 2.5 License
----------------------------------------------------------------------*/
class json {
/*--------------------------------------------------
Encode the variable/array/object
--------------------------------------------------*/
function encode($input) {
$output = $this->get(NULL, $input);
return $output;
}
/*--------------------------------------------------
Get the encoded variable
--------------------------------------------------*/
function get($key, $value, $parent = NULL) {
$type = $this->type($key, $value);
switch ($type) {
case 'object':
$value = '{'.$this->loop($value, $type).'}';
break;
case 'array':
$value = '['.$this->loop($value, $type).']';
break;
case 'number':
$value = $value;
break;
case 'string':
$value = '"'.$this->escape($value).'"';
break;
case 'boolean':
$value = ($value) ? 'true' : 'false';
break;
case 'null':
$value = 'null';
break;
}
if(!is_null($key) && $parent != 'array')
$value = '"'.$key.'":'.$value;
return $value;
}
/*--------------------------------------------------
Check the type of the variable
--------------------------------------------------*/
function type($key, $value) {
if(is_object($value)) {
$type = 'object';
}
elseif(is_array($value)) {
if($this->is_assoc($value))
$type = 'object';
else
$type = 'array';
}
elseif(is_int($value) || is_float($value)) {
$type = 'number';
}
elseif(is_string($value)) {
$type = 'string';
}
elseif(is_bool($value)) {
$type = 'boolean';
}
elseif(is_null($value)) {
$type = 'null';
}
else {
die($key.' is of an unsupported type.');
}
return $type;
}
/*--------------------------------------------------
Loop through the array/object
--------------------------------------------------*/
function loop($input, $type) {
$output = NULL;
foreach($input as $key => $value) {
$output .= $this->get($key, $value, $type).',';
}
$output = trim($output, ',');
return $output;
}
/*--------------------------------------------------
Escape strings
--------------------------------------------------*/
function escape($string) {
$find = array('//', '"', '/', "/b", "/f", "/n", "/r", "/t", "/u");
$repl = array('////', '/"', '//', '/b', '/f', '/n', '/r', '/t', '/u');
$string = str_replace($find, $repl, $string);
return $string;
}
/*--------------------------------------------------
Check if the array is associative
--------------------------------------------------*/
function is_assoc($array) {
krsort($array, SORT_STRING);
return !is_numeric(key($array));
}
}
?>
从这个站找来的,还有些相关资料,大家可以再看看
http://www.reallyshiny.com/scripts/php-json-class/
这个地址是pecl的json编译好了的dll下载,可以根据自己使用php版本来下有
http://pecl4win.php.net/ext.php/php_json.dll
具体使用办法:
$arr=array(
"name"=>"killall","sex"=>"boy");
$str=json_encode($arr);//将数组转为json格式
echo $str;
echo "<br>";
$a=json_decode($str,false);//true返回为数组,flase返回对对象,默认为flase
echo $a->name;
?>
在客户端 str=eval("(+json+")");
- ajax中,使用Json代替xml加快速度
- 提交数据使用json代替xml
- Ajax中使用JSON
- Spring中使用注解代替xml配置文件
- 加快jbuilder编辑xml的速度
- XML的代替者JSON
- 用JSON技术加快AJAX程序开发
- 用JSON技术加快AJAX程序开发
- Ajax中使用JSON格式
- 加快Cinder中Volume的删除速度
- 使用VB.NET加快代码开发速度
- 使用memcached 加快openstack keystone的速度
- 消息队列使用,异步执行,加快速度
- 使用DocumentFragment加快DOM渲染速度
- Qt使用多核(jom)加快编译速度
- 微软发布Doloto工具,加快AJAX访问速度
- Ajax中使用XML和JSON格式传输的优劣比较
- Ajax中XML和json两种数据格式的使用和比较
- 不知道怎么将一个线程添加到线程池?
- Primitive Collision Detecting (2)
- 意识流5.14
- webwok中的封装页面数据研究(下)-ModelDrivenInterceptor分析
- 启示
- ajax中,使用Json代替xml加快速度
- 重读《UML基础、案例与应用》(一)
- 如何用CSS制作横向菜单?
- netsgoo.com
- 你的定位,你的一生--解读IT人员的从业方向
- webwok中的封装页面数据研究(上)-两种实现方式
- 买东西一定要看准,而且不要贪图一点便宜!
- ASP.NET系统用户权限设计与实现
- 十寸光阴半寸铜