2017-03-06 53 views
0

JSON数据进行解码,我想用PHP从MySQL访问某些数据,然后通过一个AJAX请求的JSON字符串发送到Java脚本。但问题是在解析java脚本中的字符串后,我无法使用这些数据。 PHP代码将类数组转换为JSON字符串,然后发送它。请告诉我这样做的正确方法,如果有的话。无法在JavaScript

PHP代码是:

<?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(); 
     ?> 

客户端侧脚本是:

<!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> 

同样对于参考正在由PHP脚本生成此JSON字符串:

{ 
    "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" 
    } 
} 
+2

请侑码减少相关的部分,并指定什么是行不通的。 –

+0

使用'F12'在浏览器中打开开发conosle和检查什么是通过AJAX调用,你得到什么真正的netwework标签。 – JustOnUnderMillions

回答

0

PHP的son_encode返回一个数组,然后你需要数组索引(不仅索引)

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