2017-05-20 51 views
0

我想要檢索this.responseText中的每個元素,並將它們放入JavaScript中的HTML中。我的代碼有問題嗎?我希望我的代碼能夠幫助你理解我的問題。謝謝。 (p.s.html代碼提供,所以我不能使用jQuery。)如何在Javascript中使用PHP中的JSON數據(數組)

this.responseText的一個例子是在下面; (通過使用alert,我得到的)

{"name":"Hermione Grainger","number":"4","review":"Not as good as the NEXT book in the series, but hilarious and satisfying."}{"name":"Ronald Drumpf","number":"1","review":"Feminist propaganda!"}{"name":"Brienne of Tarth","number":"5","review":"Alanna is my best friend."} 

我的Java腳本低於;

function bookReview(){ 
    var reviews = JSON.parse(this.responseText); 
    for(var i=0; i<reviews.length; i++){ 
     var name = document.createElement("h3"); 
     var text = document.createElement("p"); 
     name.innerHTML = reviews[i].name + reviews[i].number; 
     text.innerHTML = reviews[i].review; 

     document.getElementById("reviews").appendChild(name); 
     document.getElementById("reviews").appendChild(text); 
    } 
} 

或者在我的PHP代碼中有什麼錯?

$path = "books/$book/"; 
review(glob($path . "review" . "*" . ".txt")); 

function review($reviews) { 
    foreach ($reviews as $each) { 
     $review = file($each, FILE_IGNORE_NEW_LINES); 
     $output = array (
      "name" => $review[0], 
      "number" => $review[1], 
      "review" => $review[2] 
      ); 
     header("Content-type: application/json"); 
     print(json_encode($output)); 
    } 
} 

回答

1

這看起來不是有效的JSON,應該是'[{},{},{}]''。如果你想要,你可以使用JSON驗證器,例如http://json-validator.com/

要生成JSON陣列正常,你可以這樣做:

$array = []; 
foreach ($reviews as $each) { 
    $review = file($each, FILE_IGNORE_NEW_LINES); 
    $output = array (
     "name" => $review[0], 
     "number" => $review[1], 
     "review" => $review[2] 
     ); 
    array_push($array,$review); 
} 
print(json_encode($array)); 
+0

我添加了我的PHP代碼。你能檢查我的PHP代碼嗎? – Tak

+0

在這裏看到第二個答案:http://stackoverflow.com/questions/6739871/how-to-create-an-array-for-json-using-php(在循環中生成數組,然後在它外面把它們放入一個數組和json.encode這一個)。 – aabbcccc

+0

我把它改成下面的代碼,但它並不能很好的工作... \t功能評審($點評){ \t \t的foreach($評論,因爲每個$){ \t \t \t $複習=文件(每個$ ,FILE_IGNORE_NEW_LINES); \t \t \t $輸出=陣列( \t \t \t \t陣列( \t \t \t \t \t 「名稱」=> $評論[0], \t \t \t \t \t 「數量」=> $審查[1], \t \t \t \t \t 「評論」=> $審查[2] \t \t \t \t \t) \t \t \t \t); \t \t \t \t } \t \t \t頭( 「內容類型:application/JSON」); \t \t print(json_encode($ output)); \t} – Tak

0

簡單使用本例爲Ajax:

AJAX:

$.ajax({ 
     url: 'test.php', 
     type: 'POST', 
     datatype: 'Json', 
     data: {'q': val_1}, 
     success: function (response) { 
      var newhref; 
      var newhrefid; 
      var div=document.getElementById("myDropdown"); 
      for(var i = 0; i<response.nunber_of_rows; i++){ 
      newhref= document.createElement("a"); 
      newhref.href= response.tabel[i]; 
      newhref.innerHTML= response.tabel[i]; 
      newhrefid = "idhr_"+i; 
      newhref.setAttribute('id', newhrefid); 
      div.appendChild(newhref); 
      } 
     } 
    }); 

PHP:

...//your code 
echo json_encode (array(
        'tabel'=>$tabel, 
        'nunber_of_rows'=>$nunber_of_rows 
)); 

在這個例子中Ë定義,你是使用JSON這一行代碼:

datatype = "Json"; 

在你的PHP你通過發回的數據:

echo json_encode (array(
        'tabel'=>$tabel, 
        'nunber_of_rows'=>$nunber_of_rows 
)); 

要在AJAX使用數據

response.nunber_of_rows 

從這個鏈接的例子How to use Ajax and JSON for making dropdown menu?

如果你不想使用AJAX:

PHP:

$phpArray = array(
      0 => "Mon", 
      1 => "Tue", 
      2 => "Wed", 
      3 => "Thu", 
      4 => "Fri", 
      5 => "Sat", 
      6 => "Sun", 

    ) 

JS:

var jArray= <?php echo json_encode($phpArray); ?>; 

    for(var i=0;i<6;i++){ 
     alert(jArray[i]); 
    } 

爲了讓PHP數組,你只需要做到這一點:

var jArray= <?php echo json_encode($phpArray); ?>; 

從這個鏈接Pass a PHP array to a JavaScript function

的例子

給你se JSON。解析:

var data = JSON.parse('<?php echo json_encode($data) ?>'); 

例子:

PHP:

<?php 
$books = array(
    array(
     "title" => "Professional JavaScript", 
     "author" => "Nicholas C. Zakas" 
    ), 
    array(
     "title" => "JavaScript: The Definitive Guide", 
     "author" => "David Flanagan" 
    ), 
    array(
     "title" => "High Performance JavaScript", 
     "author" => "Nicholas C. Zakas" 
    ) 
); 
?> 

JS:

<script type="text/javascript"> 
// using JSON.parse on the output of json_encode 
var books = JSON.parse('<?php echo json_encode($books); ?>'); 

/* output (with some whitespace added for readability) 
[ 
    {"title":"Professional JavaScript", "author":"Nicholas C. Zakas"}, 
    {"title":"JavaScript: The Definitive Guide", "author":"David Flanagan"}, 
    {"title":"High Performance JavaScript", "author":"Nicholas C. Zakas"} 
] 
*/ 

// how to access 
console.log(books[1].author); // David Flanagan 
</script> 

我們從你的原數組的值,你只需要使用:

books[1].author 

示例它來自此鏈接http://www.dyn-web.com/tutorials/php-js/json/parse.php

相關問題