如何将xmlhttp.responseText转换为js数组并在js中打印

How to convert xmlhttp.responseText to js array

this is my {"name":"Eswara Manikanta Varma","email":"eswar1251@gmail.com","mobile":"9966578911"} getting from xmlhttp.responseText so now i want to convert in js array.. when ever i'm alerting var object the alert seem like this [object Object] I would like to print it as jsarray[mobile]

my page 1 :

<!doctype html>
<html>
       <head>
               <meta charset="utf-8">
               <title>Invoice</title>
               <link rel="stylesheet" href="style.css">

               <script src="script.js"></script>
       <script>
function showUser(str)
{
if (str=="")
 {
 document.getElementById("txtHint").innerHTML="";
 return;
 }
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4)
   {
        var object = JSON.parse(xmlhttp.responseText);
        alert(object); 
   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

   }
 }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
       <body>
               <header>
                       <h1>Invoice</h1>
                       <address contenteditable>
                               <p>Jonathan Neal</p>
                               <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
                               <p>(800) 555-1234</p>
                       </address>
                       <span><img alt="" src="logo.png"><input type="file" accept="image/*"></span>
               </header>
               <article>
                       <h1>Recipient</h1>
                       <address contenteditable>
                               <p>Some Company<br>c/o Some Guy</p>
                       </address>
           <form>
                       <table class="meta">
                               <tr>
                                       <th><span contenteditable>Invoice #</span></th>
                                       <td><span contenteditable>101138</span></td>
                               </tr>
                               <tr>
                                       <th><span contenteditable>Date</span></th>
                                       <td><span contenteditable>January 1, 2012</span></td>
                               </tr>
                               <tr>
                                       <th><span contenteditable>Amount Due</span></th>
                                       <td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
                               </tr>
                       </table>
                       <table class="inventory">
                               <thead>
                                       <tr>
                                               <th><span contenteditable>Item</span></th>
                                               <th><span contenteditable>Description</span></th>
                                               <th><span contenteditable>Rate</span></th>
                                               <th><span contenteditable>Quantity</span></th>
                                               <th><span contenteditable>Price</span></th>
                                       </tr>
                               </thead>
</body>
</html>

page 2 :

<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost','root','enter','esmart');
if (!$con)
 {
 die('Could not connect: ' . mysqli_error($con));
 }

mysqli_select_db($con,'esmart');
$sql="SELECT * FROM suppliers WHERE name LIKE '%$q%'";

$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
 {
    $x['name']=$row['name'];
    $x['email']=$row['email'];
    $x['mobile']=$row['mobile'];
 }
echo json_encode($x);

mysqli_close($con);
?> 

I think your problem is in the PHP code. with this:

while($row = mysqli_fetch_array($result))
 {
    $x['name']=$row['name'];
    $x['email']=$row['email'];
    $x['mobile']=$row['mobile'];
 }
echo json_encode($x);

You're going to get only the last row of your query. To be able to get an array you may need something like this:

$response = array();
while($row = mysqli_fetch_array($result))
 {
    $x['name']=$row['name'];
    $x['email']=$row['email'];
    $x['mobile']=$row['mobile'];
    $response[] = $x;
 }
echo json_encode($response);

Now you'll get [{"name":"Eswara Manikanta Varma","email":"eswar1251@gmail.com","mobile":"9966578911"}, ...] and after JSON.parse it will be an array of objects.

try to use:

mysqli_fetch_assoc($result)

instead of

mysqli_fetch_array($result)