教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php中怎么把json转成对象数组

php中怎么把json转成对象数组

发布时间:2020-05-15   编辑:jiaochengji.com
教程集为您提供php中怎么把json转成对象数组等资源,欢迎您收藏本站,我们将为您提供最新的php中怎么把json转成对象数组资源

php中怎么把json转成对象数组 ?

在php中可以使用json_decode函数将json转成对象数组。

json_decode是php5.2.0之后新增的一个PHP内置函数,其作用是对JSON格式的字符串进行编码.

那么这个函数该如何使用呢?

json_decode的语法规则:

json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

json_decode接受一个JSON格式的字符串并且把它转换为PHP变量 ,当该参数$assoc为TRUE时,将返回array,否则返回object。

JSON 格式的字符串

$json = '{"a":"php","b":"mysql","c":3}';

其中a为键,php为a的键值。

实例:

<?php   
$json = '{"a":"php","b":"mysql","c":3}';  
$json_Class=json_decode($json);   
$json_Array=json_decode($json, true);   
print_r($json_Class);   
print_r($json_Array);         
?>

程序输出:

 stdClass Object (
  [a] => php
  [b] => mysql
  [c] => 3 )
  Array (
  [a] => php
  [b] => mysql
  [c] => 3 )

在上面代码的前提下

访问对象类型$json_Class的a的值

echo $json_Class->{'a'};

程序输出:php

访问数组类型$json_Array的a的值

echo $json_Array['a'];

程序输出:php

推荐:《php教程》

以上就是php中怎么把json转成对象数组的详细内容,更多请关注教程集其它相关文章!

  • 本文原创发布教程集,转载请注明出处,感谢您的尊重!
  • 您可能感兴趣的文章:
    php中怎么把json转成对象数组
    PHP怎么把JSON转换成数组?
    PHP JSON转数组
    json为什么像花儿一样红
    JQuery将文本转化成JSON对象需要注意的问题
    解决json_encode 函数中文被编码成 null的办法
    php如何将json文本转换成数组
    php怎么访问mysql的数组
    js怎么通过ajax给php发送数据
    php数组与Json转换的方法探讨

    [关闭]
    ~ ~