PHP-json使用注意点
来源:互联网 发布:eagle软件下载 编辑:程序博客网 时间:2024/05/18 01:09
json_decode — 对 JSON 格式的字符串进行解码
mixed json_decode ( string $json
[, bool $assoc
= false [, int $depth
= 512 [, int $options
= 0 ]]] )
参数
json
待解码的
json
string 格式的字符串。这个函数仅能处理 UTF-8 编码的数据。
assoc
当该参数为
TRUE
时,将返回 array 而非 object 。depth
指定递归深度。
options
JSON解码的掩码选项。 现在有两个支持的选项。 第一个是
JSON_BIGINT_AS_STRING
, 用于将大整数转为字符串而非默认的float类型。第二个是JSON_OBJECT_AS_ARRAY
, 与将assoc
设置为TRUE
有相同的效果。
返回值
Returns the value encoded in json
in appropriate PHP type. Values true, false and null are returned as TRUE
, FALSE
and NULL
respectively. NULL
is returned if the json
cannot be decoded or if the encoded data is deeper than the recursion limit.
json_decode要求的字符串比较严格:
(1)使用UTF-8编码(2)不能在最后元素有逗号
(3)不能使用单引号
(4)不能有\r,\t,如果有请替换
编码 转化
public static function changeStringEncoding($string){
$finalEncoding = 'UTF-8';
if( strlen($string) <= 0 ){
return '';
}
$encode = mb_detect_encoding($string, array('GB2312','GBK','UTF-8'));
if($encode == 'GB2312'){
$string = iconv( 'GBK', $finalEncoding , $string);
}else if($encode == 'GBK') {
$string = iconv( 'GBK' , $finalEncoding , $string);
} else if($encode == 'EUC-CN'){
$string = iconv( 'GBK' , $finalEncoding , $string);
} else {
$string = iconv( 'GB2312' , $finalEncoding ,$string);
}
return $string;
}
或 使用前确保是 UTF-8
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$results = json_decode($contents);
友情链接
http://blog.sina.com.cn/s/blog_7253e3930102wdbd.html
http://php.net/manual/zh/function.json-decode.php
- PHP-json使用注意点
- PHP 函数使用注意点
- JS使用eval解析json注意点
- eval、json.parse()的介绍和使用注意点
- php基础注意点
- php注意点
- php 注意点随记
- php 日期注意点
- php编程注意点
- json解析的注意点
- JSON.stringify 的注意点
- eval解析JSON注意点
- 对象转JSON注意点
- 使用php apc模块需要注意的几点
- 【Php】在函数内部使用unset的一些注意点
- json使用注意
- DataReader使用注意点
- JSF使用注意点
- 练习四(第三周)
- FreeRTOS config开始的宏
- 基于java web开发的在线考试系统
- Git 快速上手精华教程
- spark 共享变量
- PHP-json使用注意点
- LeetCode 62. Unique Paths
- 【模板】手读手输
- Mysql数据库表连接
- 《Drools7.0.0.Final规则引擎教程》番外实例篇——FactHandler使用案例
- 杂记(浏览器渲染)
- GOMAXPROCS相关
- 9-17 (水BFS,e24高精度, 最大流最小割, 思路T,卡AC自动机)
- iframe