很多站长朋友们都不太清楚json转换为php,今天小编就来给大家整理json转换为php,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 json文本文档里的数据用php怎么输出 2、 PHP怎么把json字符串转为对象 3、 json转换php格式 4、 json转php数组中有[list]怎么办? json文本文档里的数据用php怎么输出首先,把PHP数组中的数据写入JSON文件。
<?php
// 生成一个PHP数组
$data = array();
$data['a'] = 'test';
$data['b'] = 'bbb';
// 把PHP数组转成JSON字符串
$json_string = json_encode($data);
// 写入文件
file_put_contents('test.json', $json_string);
?>
然后,把JSON文件中的数据读取到PHP变量中。
<?php
// 从文件中读取数据到PHP变量
$json_string = file_get_contents('test.json');
// 把JSON字符串转成PHP数组
$data = json_decode($json_string, true);
// 显示出来看看
var_dump($data);
?>
PHP怎么把json字符串转为对象json_decode
PHP json_decode() 函数用于对 JSON 格式的字符串进行解码,并转换为 PHP 变量。
语法
mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
参数
json_string: 待解码的 JSON 字符串,必须是 UTF-8 编码数据
assoc: 当该参数为 TRUE 时,将返回数组,FALSE 时返回对象。
depth: 整数类型的参数,它指定递归深度
options: 二进制掩码,目前只支持 JSON_BIGINT_AS_STRING 。
json转换php格式print_r(json_decode($json,true)) 看看手册就明白了
当第二个参数 为 TRUE 时,将返回 array 而非 object 。
json转php数组中有[list]怎么办?1.fastjson List转JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
2.fastjson JSONArray转List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
3.fastjson 字符串转List
String str = "";
List<T> list = JSONObject.parseArray(str,T.class);
关于json转换为php的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于json转换为php json转换为数组的详细内容...