Json_encode()该函数主要用于将数组和对象转换为Json格式。复制代码如下: $ arr=array ('a'=' a ',' b'=' b ',' c'=' c ',' d'=' d ',' e '=' e ');echo JSON _ encode($ arr);产出结果:
Json只接受utf-8编码的字符,json_encode()的参数必须是utf-8编码的。副本代码如下:class person { public $ name公共$ age公共$ heightfunction __construct($name,$age,$ height){ $ this-name=$ name;$ this-age=$ age;$ this-height=$ height;} }$obj=新人('张三',20,100);$ foo _ JSON=JSON _ encode($ obj);echo $ foo _ json产出结果:
当类中的属性是私有变量时,它不会被输出。Json_decode()该函数用于将Json文本转换为对应的PHP数据结构。复制的代码如下: $ JSON=' {'a' :' hello ',' b' :' world ',' c' : '樟山',' d' :20,' e ' :170 }var _ dump(JSON _ decode($ JSON));产出结果:
通常,json_decode()总是返回一个PHP对象。成数组:复制代码如下: $ JSON=' {'a' :' hello ',' b' :' world ',' c' : '樟山',' d' :20,' e ' :170 }var_dump(json_decode($json,ture));