2013-11-02 57 views
-1

這裏是我的html顯示,解析JSON值在PHP和表

<html> 
<head> 
<script src="js/jquery.js"></script> 
</head> 

<body> 
<input type="text" name="query" id="queryBox"> 
<button id="load_basic" value = "button">search</button><br/> 

<div id="result"></div> 
</body> 

<script> 

    $.ajaxSetup ({ 
     cache: false 
    }); 
    var ajax_load = "<img src='images/ajax-loader.gif' alt='loading...' />"; 

    $("#load_basic").click(function(){ 
    var query = $("#queryBox").val(); 
    var loadUrl = "http://localhost:8983/solr/collection1/select?q=" + query +"&fl=title&wt=php&indent=true"; 
    $("#result").html(ajax_load).load(loadUrl); 

    }); 

</script> 

</html> 

在給查詢結果div的價值將是一個JSON,一個例子的結果會像下面,

{ 
    "responseHeader":{ 
     "status":0, 
     "QTime":58, 
     "params":{ 
     "fl":"title", 
     "indent":"true", 
     "q":"gizmodo", 
     "_":"1383358368484", 
     "wt":"json" 
     } 
    }, 
    "response":{ 
     "numFound":4, 
     "start":0, 
     "docs":[ 
     { 
      "title":"AACS encryption key controversy" 
     }, 
     { 
      "title":"LOL" 
     }, 
     { 
      "title":"MythBusters" 
     }, 
     { 
      "title":"Anonymous (group)" 
     } 
     ] 
    } 
} 

如何解析這個json並單獨獲取標題中的值?

<?php 
$query = "http://localhost:8983/solr/collection1/select?q=" . $_GET['q'] ."&fl=title&wt=json&indent=true"; 
$response = file_get_contents($query); 
echo $response; 
?> 

回答

0

在JS,解析您的div中的JSON,你可以做到這一點

var myJSON=jQuery.parseJSON(jQuery('#result').text()); 
var title=myJSON.response.docs[0].title; 
console.log(title);