简体   繁体   English

使用Jquery从PHP页面解码JSON数据

[英]Decoding JSON Data from PHP Page Using Jquery

I want to take data from database and save it in an array. 我想从数据库中获取数据并将其保存在数组中。 Like this 像这样

 var locations = [ ['Current', 18.53515053, 73.87944794, 2],

  ['VimanNagar', 18.5670762, 73.9084194, 1]
];

First of all I have created a php page 首先,我创建了一个php页面

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "citytrans";

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

$sql = "SELECT * FROM driver_location";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       echo json_encode($row);
    }
} else {
    echo "0 results";
}
$conn->close();
?>

which gives me below result 这给了我以下结果

{"driver_id":"1","driver_lat":"18.53515053","driver_lng":"73.87944794","driver_code":"122"}{"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"}

Now I want to convert this into an array using Jquery (I want to decode it ), I just want drivers_lat and drivers_lng value from my jSON data fetched form the database show above. 现在我想使用Jquery将其转换为数组(我想解码它),我只想从上面显示的数据库中获取我的jSON数据中的drivers_latdrivers_lng值。

I am using below code to parse the data form json 我使用下面的代码来解析数据形式json

jQuery.ajax({                              
    url: baseurl +  "getdriverlocation.php",
    type: "JSON",
    async: false,
    success: function(data){
           var myArray = JSON.parse(data);
                console.log(myArray.driver_lat)
    } 
});

but it is giving me error (shown below) 但它给了我错误(如下所示)

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 92 of the JSON data SyntaxError:JSON.parse:在JSON数据的第1行第92列的JSON数据之后出现意外的非空白字符

I just want the two values from json data and save it in an array variable 我只想从json数据中获取两个值并将其保存在数组变量中

Please help 请帮忙

Use this one.. 使用这一个..

jQuery.ajax({                              
url: baseurl +  "getdriverlocation.php",
type: "JSON",
async: false,
success: function(data){
       var myArray = jQuery.parseJSON(data);// instead of JSON.parse(data)
       jQuery(myArray).each(function( index, element ) {     
         console.log(element.driver_lat)
       });
} 
});

In your php you should do : 在你的PHP你应该做:

if ($result->num_rows > 0) {
    // output data of each row <- no, build your data, then make only 1 output
    $output = array();
    while($row = $result->fetch_assoc()) {
       $output[] = $row;
    }
    echo json_encode($output);
}

Then in your jQuery, parse the whole json-decoded array... 然后在你的jQuery中,解析整个json解码的数组......

Your json data is invalid. 您的json数据无效。 You must put comma bettween two JSON Objects Your respons must be {"driver_id":"1","driver_lat":"18.53515053","driver_lng":"73.87944794","driver_code":"122"} , {"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"} 你必须在两个JSON对象之间添加逗号你的响应必须是{“driver_id”:“1”,“driver_lat”:“18.53515053”,“driver_lng”:“73.87944794”,“driver_code”:“122”} {“driver_id” “2”, “driver_lat”: “18.53640175”, “driver_lng”: “73.88206482”, “driver_code”: “133”}

As i identified your Response JSON format is invalid, response JSON format should like this in order to parse into JSON via JSON.parse() 当我确定您的Response JSON格式无效时,响应JSON格式应该是这样的,以便通过JSON.parse()解析为JSON

    [{"driver_id":"1","driver_lat":"18.53515053","driver_lng":"73.87944794","driver_code":"122"},
{"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"}]

Try this 尝试这个

$arrTmp = array();
if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
      $arrTmp[] = $row;
   }
}
echo json_encode($arrTmp);

And maybe the jQuery tools bellow for old browsers 也许jQuery工具对于旧版浏览器而言

$.parseJSON(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM