douwen5741 2017-03-06 13:41
浏览 71

无法在javascript中解码JSON数据

I wanted to access some data from mysql using PHP and then send it to the java script via a AJAX request as a JSON string. But the problem is after parsing the string in java script, i am not able to use the data. The PHP code is converting array of classes to a JSON string and then sending it. Please tell me the proper way of doing this, if any.

The php code is:

<?php
            ini_set('error_reporting', E_STRICT);       //Suppress warnings. !!Disable only during developmental time
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "crie_test";

            //create connection
            $conn = new mysqli($servername,$username,$password,$dbname);
            if($conn->connect_error){
                die("Connection failed".$conn->connect_error);
            }

            class publications{
                public $dept_name,$dept_code,$int_jour, $nat_jour, $inter_nat_conf, $nat_conf, $int_book_chap, $nat_book_chap;
            }

            //Fetch department codes
            $sql = "SELECT * FROM tbl_dept_info";
            $res = $conn->query($sql);
            $no_of_dept = mysqli_num_rows($res);

            $dept_li[$no_of_dept] = new publications();         //create department object array

            //Intialize variables
            for($i=0;$i<$no_of_dept;$i++){
                $dept_li[$i]->int_jour=0;
                $dept_li[$i]->nat_jour=0;
                $dept_li[$i]->inter_nat_conf=0;
                $dept_li[$i]->nat_conf=0;
                $dept_li[$i]->int_book_chap=0;
                $dept_li[$i]->nat_book_chap=0;
            }

            if ($res->num_rows > 0) {
                $i = 0;
                while($row = $res->fetch_assoc()) {
                    $dept_li[$i]->dept_code = $row["Dept_Code"];
                    $dept_li[$i]->dept_name = $row["DeptType"];
                    $i++;
                }
            }
            else{
                echo "Unable to fetch department id's!!";
            }

            //fetch the research table
            $sql1 = "SELECT tbl_dept_research.ResearchCode, tbl_dept_research.DeptCode, tbl_research.Publication FROM tbl_dept_research INNER JOIN tbl_research ON tbl_dept_research.ResearchCode=tbl_research.ResearchCode";
            $res1 = $conn->query($sql1);


            if($res1->num_rows>0){
                while($row = $res1->fetch_assoc()) {
                    $dep_code = $row["DeptCode"];
                    for($i=0;$i<$no_of_dept;$i++){
                        if($dept_li[$i]->dept_code==$dep_code){
                            switch($row['Publication']){
                                case"International Journal": $dept_li[$i]->int_jour++;
                                                            break;
                                case"National Journal": $dept_li[$i]->nat_jour++;
                                                            break;
                                case"International Conference": $dept_li[$i]->inter_nat_conf++;
                                                            break;
                                case"National Conference": $dept_li[$i]->nat_conf++;
                                                            break;
                                case"Book Chapter International": $dept_li[$i]->int_book_chap++;
                                                            break;
                                case"Book Chapter National": $dept_li[$i]->nat_book_chap++;
                                                            break;
                            }
                        }
                    }
                }
            }

            echo json_encode($dept_li);

            $conn->close();
        ?>

The client side script is:

<!DOCTYPE html>
<html>
<body>

<h2>Get data as JSON from a PHP file on the server.</h2>

<p id="demo"></p>

<script>

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        document.getElementById("demo").innerHTML = myObj.0.dept_name;
    }
};
xmlhttp.open("GET", "sss.php", true);
xmlhttp.send();

</script>

</body>
</html>

Also for reference this JSON string is being generated by the PHP script:

{
    "8": {
        "dept_name": null,
        "dept_code": null,
        "int_jour": null,
        "nat_jour": null,
        "inter_nat_conf": null,
        "nat_conf": null,
        "int_book_chap": null,
        "nat_book_chap": null
    },
    "0": {
        "int_jour": 10,
        "nat_jour": 1,
        "inter_nat_conf": 16,
        "nat_conf": 14,
        "int_book_chap": 1,
        "nat_book_chap": 4,
        "dept_code": "101",
        "dept_name": "ECE"
    },
    "1": {
        "int_jour": 22,
        "nat_jour": 1,
        "inter_nat_conf": 32,
        "nat_conf": 16,
        "int_book_chap": 5,
        "nat_book_chap": 0,
        "dept_code": "102",
        "dept_name": "CSE"
    },
    "2": {
        "int_jour": 12,
        "nat_jour": 4,
        "inter_nat_conf": 10,
        "nat_conf": 23,
        "int_book_chap": 1,
        "nat_book_chap": 0,
        "dept_code": "103",
        "dept_name": "IT"
    },
    "3": {
        "int_jour": 21,
        "nat_jour": 0,
        "inter_nat_conf": 9,
        "nat_conf": 35,
        "int_book_chap": 0,
        "nat_book_chap": 0,
        "dept_code": "104",
        "dept_name": "EE"
    },
    "4": {
        "int_jour": 13,
        "nat_jour": 1,
        "inter_nat_conf": 8,
        "nat_conf": 33,
        "int_book_chap": 0,
        "nat_book_chap": 1,
        "dept_code": "105",
        "dept_name": "MCA"
    },
    "5": {
        "int_jour": 10,
        "nat_jour": 5,
        "inter_nat_conf": 12,
        "nat_conf": 13,
        "int_book_chap": 0,
        "nat_book_chap": 1,
        "dept_code": "106",
        "dept_name": "MBA"
    },
    "6": {
        "int_jour": 57,
        "nat_jour": 6,
        "inter_nat_conf": 5,
        "nat_conf": 10,
        "int_book_chap": 0,
        "nat_book_chap": 1,
        "dept_code": "109",
        "dept_name": "AS"
    },
    "7": {
        "int_jour": 0,
        "nat_jour": 0,
        "inter_nat_conf": 0,
        "nat_conf": 0,
        "int_book_chap": 0,
        "nat_book_chap": 0,
        "dept_code": "110",
        "dept_name": "CIVIL"
    }
}
  • 写回答

1条回答 默认 最新

  • duanchuang3182 2017-03-06 13:44
    关注

    The PHP son_encode return ana array and then You need array index (not only index)

     document.getElementById("demo").innerHTML = myObj.[0].dept_name;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来