PHP parsing JSON Data Two examples of methods
Most popular Web services like Twitter, which provides data through open APIs, always know how to parse the various delivery formats for API data, including Json,xml, and so on.
$json _string= ' {"id": 1, "name": "php100.com", "email": "PHP Tutorial" >admin@php100.com "," Interest ": [" WordPress "," PHP "]} ';
$obj =json_decode ($json _string);
Echo $obj->name; Prints Foo
Echo $obj->interest[1]; Prints PHP
Ecshop parsing JSON classes
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 ' and ' u0002 ', ' x03 ' and ' u0003 ',
"x04" = ' u0004 ', "x05" = ' u0005 ', ' x06 ' and ' u0006 ', ' x07 ' and ' u0007 ',
"x08" = ' B ', "x0b" = ' u000b ', ' x0c ' = ' f ', ' x0e ' = ' u000e ',
"x0f" = ' u000f ', "x10" = ' u0010 ', ' x11 ' and ' u0011 ', ' x12 ' and ' u0012 ',
"X13" = ' u0013 ', "x14" = ' u0014 ', ' x15 ' and ' u0015 ', ' x16 ' and ' u0016 ',
"X17" = ' u0017 ', "x18" = ' u0018 ', ' x19 ' and ' u0019 ', ' x1a ' and ' u001a ',
"x1b" = ' u001b ', "x1c" = ' u001c ', ' x1d ' and ' u001d ', ' x1e ' and ' u001e ',
"x1f" = ' u001f '
)) . '"';
Break
Case ' Boolean ':
$returnValue = $arg? ' True ': ' false ';
Break
Default
$returnValue = ' null ';
}
return $returnValue;
}
function decode ($text, $type =0)//default type=0 return obj,type=1 return 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 tutorial Lashes ($text), $type));
}
$this->at = 0;
$this->ch = ";
$this->text = Strtr (Stripslashes ($text), Array (
"r" + = "," n "=", "t" + = "," b "= =",
"x00" "=", "x01" and "", "x02" and "", "x03" and "
"x04" "=", "x05" and "", "x06" and "", "x07" and "
"x08" "=", "x0b" and "", "x0c" and "", "x0e" and "
"x0f" "=", "x10" and "", "x11" and "", "x12" and "
"X13" "=", "x14" and "", "x15" and "", "x16" and "
"X17" "=", "x18" and "", "x19" and "", "x1a" and "
"x1b" "=", "x1c" and "", "x1d" and "", "x1e" and "
"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 < 4; $i + +)
{
$t = (integer) sprintf ('%01c ', Hexdec ($this->next ()));
if (!is_numeric ($t))
{
Break 2;
}
$u = $u * + $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->nex T () = = ' 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;
}
/**
* The recursive way to escape special characters in a variable
*
* @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);
}
}
/**
* Escape the special characters of an object member variable or an array
*
* @access Public
* @param mix $obj object or array
* @author Xuan Yan
*
* @return Mix object or array
*/
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;
}
/**
* Recursive method to remove escape from special characters in variable
*
* @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);
}
}
/**
* Transcoding of parameters passed by 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 ': ' $ ', ' Enabled_captcha ': ' 0 ', ' Captcha ":" "," Rank ":" 5 "} ';
$json =new json ();
$CMT = $json->json_str_iconv ($string); Character transcoding
$CMT = $json->decode ($CMT); Decoding
Print_r ($CMT);
http://www.bkjia.com/PHPjc/444936.html www.bkjia.com true http://www.bkjia.com/PHPjc/444936.html techarticle PHP parsing JSON data Two instance methods most popular Web services like Twitter provide data through an open API, and it always knows how to parse the various delivery formats of API data ...