美文网首页
php中json_decode()和json_encode()的

php中json_decode()和json_encode()的

作者: followyounger1 | 来源:发表于2017-08-17 14:57 被阅读12次

    json_decode($data,true)输出的一个关联数组,由此可知json_decode($data)输出的是对象,而json_decode("$arr",true)是把它强制生成PHP关联数组.
    $data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]';
    echo json_decode($data);

    Array ( [0] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) )

    echo json_decode($data,true);

    Array ( [0] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) )

    json_encode()和json_decode()是编译和反编译过程,注意json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者null。
    $obj->Name= 'a1';$obj->Number ='123';
    $obj->Contno= '000';
    echo json_encode($obj);

    {"Name":"a1",
    "Number":"123",
    "Contno":"000"
    }

    相关文章

      网友评论

          本文标题:php中json_decode()和json_encode()的

          本文链接:https://www.haomeiwen.com/subject/dcamrxtx.html