PHP中解析json,该怎么解决

WBOY
Release: 2016-06-13 11:57:13
Original
1021 people have browsed it

PHP中解析json
我从一个页面获取到一个json字符串,然后将它转换为json数组再插入到数据库中,但插进去的都是空值,我确定能获取到json字符串。
    include_once('inc/conn.php');
    $json_string=$_REQUEST["visitor"];
    $addressInfo=json_decode( $json_string,TRUE);
        $name=$addressInfo['name'];
        $idcard=$addressInfo['idcrd']; 
        $visitedphone=$addressInfo['visitedphone'];
        $address=$addressInfo['address'];
        $visitedorganization=$addressInfo['visitedorganization'];
        echo"1$visitedphone";
        echo"2$address";
        echo"3$visitedorganization";
        $sql="insert into adress_list(name,idcrad,tel,adress,visitorganization)values('$name','$idcard','$visitedphone','$address','$visitedorganization'); ";
        $rest=0;
        $rest = mysql_query($sql);
        echo mysql_error();
        if($rest){
            echo "恭喜,上传成功!";
        }else{
            echo "上传失败,请重新上传!";
        }
    //关闭数据库 
    mysql_close($con);         

?>
这是我的代码   求大神指导,急求。。。
------解决方案--------------------
$name =  $addressInfo['visitors'][0]['name'];  这样才行。
------解决方案--------------------
少了一层  foreach 一下你就看到了
------解决方案--------------------
应该这样写
$addressInfo=json_decode( $json_string,TRUE);
$addressInfo=['visitors'][0];
一下是你原来的代码

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template