在看ecshop源码时,看到json这个类,研究了一下,它是为了兼容低版本php而做出的类,是对php的数据和json转换时使用的。

encode和decode函数是对json的操作,对应json_encode和json_decode函数。

<?php

/**
* ECSHOP JSON

* $Author: liubo $
* $Id: cls_json.php 17217 2011-01-19 06:29:08Z liubo $
*/

class JSON
{
var $at = 0;
var $ch = '';
var $text = '';

function encode($arg, $force = true)
{
static $_force;
if (is_null($_force))
{
$_force = $force;
}

if ($_force && EC_CHARSET == 'utf-8' && function_exists('json_encode'))
{
return json_encode($arg);
}

$returnValue = '';
$c = '';
$i = '';
$l = '';
$s = '';
$v = '';
$numeric = true;

switch (gettype($arg))
{
case 'array':
foreach ($arg AS $i => $v)
{
if (!is_numeric($i))
{
$numeric = false;
break;
}
}

if ($numeric)
{
foreach ($arg AS $i => $v)
{
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($arg[$i]);
}

$returnValue = '[' . $s . ']';
}
else
{
foreach ($arg AS $i => $v)
{
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($i) . ':' . $this->encode($arg[$i]);
}

$returnValue = '{' . $s . '}';
}
break;

case 'object':
foreach (get_object_vars($arg) AS $i => $v)
{
$v = $this->encode($v);

if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($i) . ':' . $v;
}

$returnValue = '{' . $s . '}';
break;

case 'integer':
case 'double':
$returnValue = is_numeric($arg) ? (string) $arg : 'null';
break;

case 'string':
$returnValue = '"' . strtr($arg, array(
"\r" => '\\r', "\n" => '\\n', "\t" => '\\t', "\b" => '\\b',
"\f" => '\\f', '\\' => '\\\\', '"' => '\"',
"\x00" => '\u0000', "\x01" => '\u0001', "\x02" => '\u0002', "\x03" => '\u0003',
"\x04" => '\u0004', "\x05" => '\u0005', "\x06" => '\u0006', "\x07" => '\u0007',
"\x08" => '\b', "\x0b" => '\u000b', "\x0c" => '\f', "\x0e" => '\u000e',
"\x0f" => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011', "\x12" => '\u0012',
"\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016',
"\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001a',
"\x1b" => '\u001b', "\x1c" => '\u001c', "\x1d" => '\u001d', "\x1e" => '\u001e',
"\x1f" => '\u001f'
)) . '"';
break;

case 'boolean':
$returnValue = $arg?'true':'false';
break;

default:
$returnValue = 'null';
}

return $returnValue;
}

function decode($text,$type=0) // 榛樿?type=0杩斿洖obj,type=1杩斿洖array
{
if (empty($text))
{
return '';
}
elseif (!is_string($text))
{
return false;
}

if (EC_CHARSET === 'utf-8' && function_exists('json_decode'))
{
return addslashes_deep_obj(json_decode(stripslashes($text),$type));
}

$this->at = 0;
$this->ch = '';
$this->text = strtr(stripslashes($text), array(
"\r" => '', "\n" => '', "\t" => '', "\b" => '',
"\x00" => '', "\x01" => '', "\x02" => '', "\x03" => '',
"\x04" => '', "\x05" => '', "\x06" => '', "\x07" => '',
"\x08" => '', "\x0b" => '', "\x0c" => '', "\x0e" => '',
"\x0f" => '', "\x10" => '', "\x11" => '', "\x12" => '',
"\x13" => '', "\x14" => '', "\x15" => '', "\x16" => '',
"\x17" => '', "\x18" => '', "\x19" => '', "\x1a" => '',
"\x1b" => '', "\x1c" => '', "\x1d" => '', "\x1e" => '',
"\x1f" => ''
));

$this->next();
$return = $this->val();

$result = empty($type) ? $return : $this->object_to_array($return);

return addslashes_deep_obj($result);
}

/**
* triggers a PHP_ERROR
*
* @access private
* @param string $m error message
*
* @return void
*/
function error($m)
{
echo($m . ' at offset ' . $this->at . ': ' . $this->text);
}

/**
* returns the next character of a JSON string
*
* @access private
*
* @return string
*/
function next()
{
$this->ch = !isset($this->text{$this->at}) ? '' : $this->text{$this->at};
$this->at++;

return $this->ch;
}

/**
* handles strings
*
* @access private
*
* @return void
*/
function str()
{
$i = '';
$s = '';
$t = '';
$u = '';

if ($this->ch == '"')
{
while ($this->next() !== null)
{
if ($this->ch == '"')
{
$this->next();

return $s;
}
elseif ($this->ch == '\\')
{
switch ($this->next())
{
case 'b':
$s .= '\b';
break;

case 'f':
$s .= '\f';
break;

case 'n':
$s .= '\n';
break;

case 'r':
$s .= '\r';
break;

case 't':
$s .= '\t';
break;

case 'u':
$u = 0;

for ($i = 0; $i < 4; $i++)
{
$t = (integer) sprintf('%01c', hexdec($this->next()));

if (!is_numeric($t))
{
break 2;
}
$u = $u * 16 + $t;
}

$s .= chr($u);
break;
case '\'':
$s .= '\'';
break;
default:
$s .= $this->ch;
}
}
else
{
$s .= $this->ch;
}
}
}

$this->error('Bad string');
}

/**
* handless arrays
*
* @access private
*
* @return void
*/
function arr()
{
$a = array();

if ($this->ch == '[')
{
$this->next();

if ($this->ch == ']')
{
$this->next();

return $a;
}

while (isset($this->ch))
{
array_push($a, $this->val());

if ($this->ch == ']')
{
$this->next();

return $a;

}
elseif ($this->ch != ',')
{
break;
}

$this->next();

}

$this->error('Bad array');
}
}

/**
* handles objects
*
* @access public
*
* @return void
*/
function obj()
{
$k = '';
$o = new StdClass();

if ($this->ch == '{')
{
$this->next();

if ($this->ch == '}')
{
$this->next();

return $o;
}

while ($this->ch)
{
$k = $this->str();

if ($this->ch != ':')
{
break;
}

$this->next();
$o->$k = $this->val();

if ($this->ch == '}')
{
$this->next();

return $o;
}
elseif ($this->ch != ',')
{
break;
}

$this->next();
}
}

$this->error('Bad object');
}

/**
* handles objects
*
* @access public
*
* @return void
*/
function assoc()
{
$k = '';
$a = array();

if ($this->ch == '<')
{
$this->next();

if ($this->ch == '>')
{
$this->next();

return $a;
}

while ($this->ch)
{
$k = $this->str();

if ($this->ch != ':')
{
break;
}

$this->next();
$a[$k] = $this->val();

if ($this->ch == '>')
{
$this->next();

return $a;
}
elseif ($this->ch != ',')
{
break;
}

$this->next();
}
}

$this->error('Bad associative array');
}

/**
* handles numbers
*
* @access private
*
* @return void
*/
function num()
{
$n = '';
$v = '';

if ($this->ch == '-')
{
$n = '-';
$this->next();
}

while ($this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
$this->next();
}

if ($this->ch == '.')
{
$n .= '.';

while ($this->next() && $this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
}
}

if ($this->ch == 'e' || $this->ch == 'E')
{
$n .= 'e';
$this->next();

if ($this->ch == '-' || $this->ch == '+')
{
$n .= $this->ch;
$this->next();
}

while ($this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
$this->next();
}
}

$v += $n;

if (!is_numeric($v))
{
$this->error('Bad number');
}
else
{
return $v;
}
}

/**
* handles words
*
* @access private
*
* @return mixed
*/
function word()
{
switch ($this->ch)
{
case 't':

if ($this->next() == 'r' && $this->next() == 'u' && $this->next() == 'e')
{
$this->next();

return true;
}
break;

case 'f':
if ($this->next() == 'a' && $this->next() == 'l' && $this->next() == 's' && $this->next() == 'e')
{
$this->next();

return false;
}
break;

case 'n':
if ($this->next() == 'u' && $this->next() == 'l' && $this->next() == 'l')
{
$this->next();

return null;
}
break;
}

$this->error('Syntax error');
}

/**
* generic value handler
*
* @access private
*
* @return mixed
*/
function val()
{
switch ($this->ch)
{
case '{':
return $this->obj();

case '[':
return $this->arr();

case '<':
return $this->assoc();

case '"':
return $this->str();

case '-':
return $this->num();

default:
return ($this->ch >= '0' && $this->ch <= '9') ? $this->num() : $this->word();
}
}

/**
* Gets the properties of the given object recursion
*
* @access private
*
* @return array
*/
function object_to_array($obj)
{
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val)
{
$val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}
}

?>

ecshop类的解析2 json_encode和json_decode的具体实现的更多相关文章

  1. ecshop类的解析1

    前面写了一下我理解的ecshop数据库表,现在看一下我理解的ecshop的类. ecshop类,ECS是一个基础类,它的取得域名的函数我感觉是比较不错的. function get_domain() ...

  2. php中的json_encode()和json_decode()函数的一些说明

    一,json语法( php中的json_decode($json)中的$json要符合json语法格式 ) ① JSON可以表示三种类型的值 1,简单值.包括整型,字符串型,布尔值和null.例如:5 ...

  3. php json_encode()和json_decode()

    json_encode()和json_decode()分别是编译和反编译过程 注意json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者nu ...

  4. PHP数组和Json之间的互相转换 json_encode() 和 json_decode()

    之所以要用到Json,很多时候是因为使用ajax对象时,程序与JS函数之间的数据交互.因为JS不认识PHP中的数组,PHP也不认识JS中的数组或对象.Json很好的解决了这个问题. Json简介 JS ...

  5. 如何利用.Net内置类,解析未知复杂Json对象

    如何利用.Net内置类,解析未知复杂Json对象 如果你乐意,当然可以使用强大的第三方类库Json.Net中的JObject类解析复杂Json字串 . 我不太希望引入第三方类库,所以在.Net内置类J ...

  6. Java 8 Optional 类深度解析

    Java 8 Optional 类深度解析 身为一名Java程序员,大家可能都有这样的经历:调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法.我们首先要判断这个返回值是否为null,只 ...

  7. javap -- Java 类文件解析器

    参考文档 http://blog.chinaunix.net/uid-692788-id-2681132.html http://docs.oracle.com/javase/7/docs/techn ...

  8. json_encode和json_decode和isset和array_key_exists

    1.json_decode() json_decode — 对 JSON 格式的字符串进行编码         说明 mixed json_decode ( string $json [, bool ...

  9. 解析HTML文件 - 运用SgmlReader类来解析HTML文件

    运用.NET Framework类来解析HTML文件.读取数据并不是最容易的.虽然你可以用.NET Framework中的许多类(如StreamReader)来逐行解析文件,但XmlReader提供的 ...

随机推荐

  1. docloud后台管理项目(前端篇)

    以下内容与主题无关,如果不想看可以直接忽视 !--忽视开始--! 给大家推荐一款强大的编辑器,那就是集响应快.体验好.逼格高.功能丰富为一体的sublime text 3.它除了以上特点,还有一个最重 ...

  2. ADODB.Stream

    读写文本文件时出现了乱码,找到了ADODB.Stream,可以指定字符集读取文本 Function ReadUTF() Filename = "F:\vba\2018 - new\2018- ...

  3. Web前端性能优化——提高页面加载速度

    前言:  在同样的网络环境下,两个同样能满足你的需求的网站,一个“Duang”的一下就加载出来了,一个纠结了半天才出来,你会选择哪个?研究表明:用户最满意的打开网页时间是2-5秒,如果等待超过10秒, ...

  4. animation与transition区别

    transition: 过渡属性 过渡所需要时间 过渡动画函数 过渡延迟时间:默认值分别为:all 0 ease 0 1.局限性: 1)只能设置一个属性 2)需要伪类/事件触发才执行 3)只能设置动画 ...

  5. 人工智能时代,你为什么一定要学Python?

    Python岗位年薪至少在10-20w之间,而且除了北.上.广.深外,杭州和合肥等二.三线城市的待遇正在与一线城市持平.未来,无论你身处何地,都能享受到人工智能.Python“带来的“市场红利”. P ...

  6. iptables详解(3):iptables规则管理

    所属分类:IPtables  Linux基础 在本博客中,从理论到实践,系统的介绍了iptables,如果你想要从头开始了解iptables,可以查看iptables文章列表,直达链接如下 iptab ...

  7. 1、DataGridView

    DataGridView赋值后 通过RowPostPaint事件绘制行号 private void AddXh() { DataGridViewTextBoxColumn col = new Data ...

  8. enote笔记语言(4)(ver0.2)——“5w1h2k”分析法

    章节:“5w1h2k”分析法   what:我想知道某个“关键词(keyword)”(即,词汇.词语,或称单词,可以是概念|专业术语|.......)的定义. why:我想知道事物发生的原因.“why ...

  9. redis下载安装配置教程

    参考 https://www.cnblogs.com/taostaryu/p/9481749.html 上面做完后, 打开客户端 $ redis-cli 以上命令将打开以下终端: redis 127. ...

  10. Oracle存储过程及函数的练习题

    --存储过程.函数练习题 --(1)创建一个存储过程,以员工号为参数,输出该员工的工资create or replace procedure p_sxt1(v_empno in emp.empno%t ...