[英]Convert a Ajax Json String to a JS Object for my Ajax Get Script
I ideally want the Ajax result to be converted from Jsonstring to OBJ Thank You in advance.理想情况下,我希望将 Ajax 结果从 Jsonstring 转换为 OBJ 提前谢谢。
I know the AJAX GET script is working becuase when I alert the Ajax Post result I see the Contents in json string format as below.我知道 AJAX GET 脚本正在工作,因为当我提醒 Ajax 发布结果时,我看到 json 字符串格式的内容如下。
alert(JSON.stringify(data));
[{"id":"1","username":"jiten","name":"Jitensingh\t","email":"jiten93mail”},{“id":"2","username":"kuldeep","name":"Kuldeep","email":"kuldeemail”}]
I want the AJAX GET result data converted to look like this in OBJ format like below.我希望将 AJAX GET 结果数据转换为如下所示的 OBJ 格式。
{id:31,name:"Mary",username:"R8344",email:"wemail}];
PHP/SQL CODE with the Json encoded Array带有 Json 编码数组的 PHP/SQL 代码
<?php
include "../mytest/config.php";
$return_arr = array();
$sql = "SELECT * FROM users ORDER BY NAME";
$result = $conn->query($sql);
//Check database connection first
if ($conn->query($sql) === FALSE) {
echo 'database connection failed';
die();
} else {
while($row = $result->fetch_array()) {
$id = $row['id'];
$username = $row['username'];
$name = $row['name'];
$email = $row['email'];
$return_arr[] = array(
"id" => $id,
"username" => $username,
"name" => $name,
"email" => $email);
}
// Encoding array in JSON format
echo json_encode($return_arr);
}
?>
php echo _encode array above returns below Json string format上面的 php echo _encode 数组返回下面的 Json 字符串格式
[{"id":"1","username":"jiten","name":"Jitensingh\t","email":"jiten93mail”},{“id":"2","username":"kuldeep","name":"Kuldeep","email":"kuldeemail”}]
I am looking for something like below.( top half of the script)我正在寻找类似下面的东西。(脚本的上半部分)
<script>
$(document).ready(function(){
$.ajax({
url: 'ajaxfile.php',
type: 'get',
dataType: 'JSON',
success: function(result){
var data =(JSONstring convert to OBJ(result);
//-----The top half of script -------------
$.each(data, function( i, person ) {
if(i == 0) {
$('.card').find('.person_id').text(person.id);
$('.card').find('.person_name').text(person.name);
$('.card').find('.person_username').text(person.username);
$('.card').find('.person_email').text(person.email);
} else {
var personDetailCloned = $('.card').first().clone();
personDetailCloned.find('.person_id').text(person.id);
personDetailCloned.find('.person_name').text(person.name);
personDetailCloned.find('.person_username').text(person.username);
personDetailCloned.find('.person_email').text(person.email);
$('.card-container').append(personDetailCloned);
}
});
});
</script>
I will need help with the closing tags as above is just an example我需要关于结束标签的帮助,因为上面只是一个例子
The solution is:解决方案是:
success: function(result){
data =(result);
There was no need o convert the data to OBJ or anything ( blush).无需将数据转换为 OBJ 或任何东西(脸红)。 Then the code on the 2nd half of the Ajax script will receive the data and populate.然后 Ajax 脚本第二半部分的代码将接收数据并填充。 Thanks to all contributors.感谢所有贡献者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.