Most popular Web services, such as twitter, provide data through open APIs, always know how to parse various transfer formats of API data, including JSON and XML.
$ Json_string = '{"id": 1, "name": "111cn.net", "email": "admin@111cn.net", "interest": ["wordpress ", "php"]} ';
$ Obj = json_decode ($ json_string );
Echo $ obj-> name; // prints foo
Echo $ obj-> interest [1]; // prints php
Ecshop json parsing class
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 _ encoding '))
{
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', "" =>' \ n', "" => '\ t', "" =>' \ B ',
"" => '\ F',' \ '=>' \ ',' "'=> '"',
"" => 'U000000', "" => 'u0001 ', "" => 'u0002', "" => 'u0003 ',
"" => 'U0004', "" => 'u0005 ', "" => 'u0006', "" => 'u0007 ',
"" => ''," "=> 'U000b'," "=>'', "" => 'u000e ',
"" => 'U000f', "" => 'u0010', "" => 'u0011', "" => 'u0012 ',
"" => 'U0013', "" => 'u0014', "" => 'u0015', "" => 'u0016 ',
"" => 'U0017', "" => 'u0018', "" => 'u0019', "" => 'u001a ',
"" => 'U001b ', "" => 'u001c', "" => 'u001d ', "" => 'u001e ',
"" => 'U001f'
)).'"';
Break;
Case 'boolean ':
$ ReturnValue = $ arg? 'True': 'false ';
Break;
Default:
$ ReturnValue = 'null ';
}
Return $ returnValue;
}
Function decode ($ text, $ type = 0) // the default type = 0 returns obj, type = 1 returns 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 (
"" => '',
"" => '',
"" => '',
"" => '',
"" => '',
"" => '',
"" => '',
"" => '',
"" =>''
));
$ 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-> })? '': $ This-> text {$ this-> };
$ 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. = '';
Break;
Case 'F ':
$ S. = '';
Break;
Case 'n ':
$ S. = '';
Break;
Case 'r ':
$ S. = '';
Break;
Case 'T ':
$ S. = '';
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 $;
}
While (isset ($ this-> ch ))
{
Array_push ($ a, $ this-> val ());
If ($ this-> ch = ']')
{
$ This-> next ();
Return $;
}
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 $;
}
While ($ this-> ch)
{
$ K = $ this-> str ();
If ($ this-> ch! = ':')
{
Break;
}
$ This-> next ();
$ A [$ k] = $ this-> val ();
If ($ this-> ch = '> ')
{
$ This-> next ();
Return $;
}
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;
}
/**
* Recursively 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 object member variables or special characters in arrays
*
* @ 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;
}
/**
* Remove escape characters from special characters in a variable in recursion mode
*
* @ 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 );
}
}
/**
* Transcode the parameters passed in 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); // character transcoding
$ Cmt = $ json-> decode ($ cmt); // decode
Print_r ($ cmt );