×

json_decode c js

json_decode(json_decode的函数定义)

admin admin 发表于2022-09-05 06:15:27 浏览141 评论0

抢沙发发表评论

本文目录

json_decode的函数定义


mixed json_decode ( string $json [, bool $assoc ] )
接受一个JSON 格式的字符串并且把它转换为 PHP 变量 json
待解码的 json string 格式的字符串。
assoc
当该参数为 TRUE 时,将返回array而非 object 。 返回一个对象,如果assoc参数选项为true,将会返回一个关联数组。


php json_decode()问题,为什么是空值


原因是你在解码时没有设置MemoryStream中流的位置,改成下面这样就行了,还有MemoryStream构造方法里的那个长度指的是字节数组的长度,如果你在编码时使用的是Unicode编码的话,解码的时候就不能得到原始的字符串了。

string XML = “asdasdasd“;
System.IO.MemoryStream XMLStream = new System.IO.MemoryStream(XML.Length);
XMLStream.Write(ASCIIEncoding.ASCII.GetBytes(XML), 0, XML.Length);
System.IO.StreamReader Reader = new System.IO.StreamReader(XMLStream);
XMLStream.Position = 0;
String Output = Reader.ReadToEnd();
建议改为
string XML = “asdasdasd“;
byte byteArr = Encoding.ASCII.GetBytes(XML);
System.IO.MemoryStream XMLStream = new System.IO.MemoryStream(byteArr.Length);
XMLStream.Write(byteArr, 0, byteArr.Length);
XMLStream.Position = 0;
System.IO.StreamReader Reader = new System.IO.StreamReader(XMLStream, Encoding.ASCII);
String Output = Reader.ReadToEnd();

为什么json_decode(’1’) 是 1,但是json_decode(’s’) 是 null


整型的1 json数据应该是“1“ 字符串的1 json数据应该是““1““ 而字符串的s 的json数据是““s““, 所以你的“s“不是json数据,会报错,所以是null,报错信息可以用var_dump(json_last_error());输出查看
-js

PHP json_decode(); 为什么没输出


我们知道网络api并不是百分百可靠,当遇到网络不稳定,接口出错等各种因素,并不一定返回标准的json数据,例如:

//如果从网络api抓取失败,返回的response为错误信息
$response = ’request timeout’;
//json_decode之后返回NULL,你打印之后仍然没有输出,所以你采纳的那个答案仍然也帮不到你
print_r(json_decode($response, true));
//使用var_dump打印虽然可以看到NULL值,可是这并没有任何用处
var_dump(json_decode($response, true));

系统函数json_decode()在解析非json字符串返回NULL,解析乱码的json也返回NULL,你压根都不知道到底是哪里出了问题,因为NULL该返回值是不能通过print_r打印出来的,而官方php手册给了一些捕获异常信息的方法已经封装在下面的函数中了:-c

《?php
/**
 * json解析接口,优化json_decode对数组的解析
 * @param   string      $json_code json字符串
 * @return  mix         错误信息或者数组
 */
function _json_decode($json_code)
{
    $json_arr = json_decode($json_code ,true);
    switch (json_last_error()){
        case JSON_ERROR_NONE:
            $error = ’’;
        break;
        case JSON_ERROR_DEPTH:
            $error = ’ - Maximum stack depth exceeded’;
        break;
        case JSON_ERROR_STATE_MISMATCH:
            $error = ’ - Underflow or the modes mismatch’;
        break;
        case JSON_ERROR_CTRL_CHAR:
            $error = ’ - Unexpected control character found’;
        break;
        case JSON_ERROR_SYNTAX:
            $error = ’ - Syntax error, malformed JSON’;
        break;
        case JSON_ERROR_UTF8:
            $error = ’ - Malformed UTF-8 characters, possibly incorrectly encoded’;
        break;
        default:
            $error = ’ - Unknown error’;
        break;
    }
    if($error)
    {
        return $error.’:’.$json_code;
    }else{
        return $json_arr;
    }
}
header(’Content-type:text/html;charset=utf-8’);
$json = ’this is not a json code’;
$arr = _json_decode($json);
if(is_array($arr))
{
    //解析正确,逻辑代码部分
    print_r($arr);
}else{
    //请求超时, 一般都会有异常处理机制, 例如重新发起请求
if($result == ’request timeout’) {
//发起请求
}else{
//如果没有异常处理机制,抛出异常给上一层处理
throw new Exception($result);
}
}

使用_json_decode的返回值如果是字符串,说明在解析json数据时遇到了错误,只要添加适当处理机制,可以大大提高代码的稳固性。-js

如果返回值是数组,这时便可以使用print_r打印出来。


json_encode json_decode 代码编写


$arr=array(
’610’=》array(
’id’=》3171,
’num’=》8,
’money’=》1
),
’629’=》array(
’id’=》2486,
’num’=》5,
’money’=》1
),
’Count’=》13
);
$arr=json_encode($arr); //将数组编码成json格式
$item=addslashes($arr);
echo $item.“《br/》“; //输出你的原本字符串
$a=json_decode($arr); //解码json数据
echo $a-》Count; //得到Count的值
-c

json_decode的实例说明


用json_decode()函数将JSON 格式的字符串编码。
《?php
$json = ’{a:1,b:2,c:3,d:4,e:5}’;
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?》
以上例程会输出:
object(stdClass)#1 (5) {
[a] =》 int(1)
[b] =》 int(2)
[c] =》 int(3)
[d] =》 int(4)
[e] =》 int(5)
}
array(5) {
[a] =》 int(1)
[b] =》 int(2)
[c] =》 int(3)
[d] =》 int(4)
[e] =》 int(5)
}-js


怎样拿到json_decode处理过的json数据值


前端直接反序列化这个josn串,var jsonobj= JSON.prase(放字符串);
读取属性就简单了,jsonobj.result.data 就是你要的数据

json_decode 空白 php


注意:This function only works with UTF-8 encoded data.
该函数只支持utf-8的数据
json_decode返回值(原文):
Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) 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.
翻译:
将true转为TRUE,false转为FALSE,null为NULL
如果json不能被解码或者编码的数据太深(默认是512),返回null
有问题请先看文档
-c

PHP如何将json值转为数组 (json_decode($json,true) 报错)


$json = 《《《EOL
{“resp“:{“respCode“:“000000“,“templateSMS“:{“createDate“:“20170117181618“,“smsId“:“8c2c520a39bcddd51f50032ce7f699f4“}}}
EOL;
var_dump(json_decode($json, true));
-js

为什么json_encode的数组,用json_decode,还原成数组之后只留下一个值了


一个办法,先用json_decode解析成对象,然后遍历对象生成数组
如果不支持json_decode函数的话,只能用分割字符串,或者正则匹配了。
但是匹配起来很麻烦
PS 。。你说的是这个json的php数组表现形式么?如果是的话 就是这样的格式
$json = array
(
array
(
’name’ =》 ’家电’,
’open’ =》 false,
’nodes’ =》 array
(
array(’name’=》’电视’),
array(’name’=》’冰箱’),
array(’name’=》’空调’,isParent=》true)
)
),
array
(
)
// more here
);
-c