php解析json数据二种实例方法
大多数流行的 Web 服务如 twitter 通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据的各种传送格式,包括 JSON,XML 等等。
$json_string='{"id":1,"name":"php100.com","email":"php教程">admin@php100.com","interest":["wordpress","php"]} ';
$obj=json_decode($json_string);
echo $obj->name; //prints foo
echo $obj->interest[1]; //prints php
ecshop解析json类
if (!defined('EC_CHARSET'))
{
define('EC_CHARSET', 'utf-8');
}
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 $this->addslashes_deep_obj(json_decode(strips教程lashes($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)
{
trigger_error($m . ' at offset ' . $this->at . ': ' . $this->text, E_USER_ERROR);
}
/**
* 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 {
$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 {
$n .= $this->ch;
$this->next();
}
if ($this->ch == '.')
{
$n .= '.';
while ($this->next() && $this->ch >= '0' && $this->ch {
$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 {
$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 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;
}
/**
* 递归方式的对变量中的特殊字符进行转义
*
* @access public
* @param mix $value
*
* @return mix
*/
function addslashes_deep($value)
{
if (empty($value))
{
return $value;
}
else
{
return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
}
}
/**
* 将对象成员变量或者数组的特殊字符进行转义
*
* @access public
* @param mix $obj 对象或者数组
* @author Xuan Yan
*
* @return mix 对象或者数组
*/
function addslashes_deep_obj($obj)
{
if (is_object($obj) == true)
{
foreach ($obj AS $key => $val)
{
$obj->$key =$this-> addslashes_deep($val);
}
}
else
{
$obj = addslashes_deep($obj);
}
return $obj;
}
/**
* 递归方式的对变量中的特殊字符去除转义
*
* @access public
* @param mix $value
*
* @return mix
*/
function stripslashes_deep($value)
{
if (empty($value))
{
return $value;
}
else
{
return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
}
}
/**
* 将JSON传递的参数转码
*
* @param string $str
* @return string
*/
function json_str_iconv($str)
{
if (EC_CHARSET != 'utf-8')
{
if (is_string($str))
{
return ecs_iconv('utf-8', EC_CHARSET, $str);
}
elseif (is_array($str))
{
foreach ($str as $key => $value)
{
$str[$key] = json_str_iconv($value);
}
return $str;
}
elseif (is_object($str))
{
foreach ($str as $key => $value)
{
$str->$key = json_str_iconv($value);
}
return $str;
}
else
{
return $str;
}
}
return $str;
}
}
$string='{"email":"cc@126.com","content":"this is a just a test","type":"0","id":"13","enabled_captcha":"0","captcha":"","rank":"5"}';
$json=new JSON();
$cmt = $json->json_str_iconv($string); //字符转码
$cmt = $json->decode($cmt); //解码
print_r($cmt);

PHP仍然流行的原因是其易用性、灵活性和强大的生态系统。1)易用性和简单语法使其成为初学者的首选。2)与web开发紧密结合,处理HTTP请求和数据库交互出色。3)庞大的生态系统提供了丰富的工具和库。4)活跃的社区和开源性质使其适应新需求和技术趋势。

PHP和Python都是高层次的编程语言,广泛应用于Web开发、数据处理和自动化任务。1.PHP常用于构建动态网站和内容管理系统,而Python常用于构建Web框架和数据科学。2.PHP使用echo输出内容,Python使用print。3.两者都支持面向对象编程,但语法和关键字不同。4.PHP支持弱类型转换,Python则更严格。5.PHP性能优化包括使用OPcache和异步编程,Python则使用cProfile和异步编程。

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver Mac版
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境