简体   繁体   English

将 Ajax Json 字符串转换为我的 Z3EB7106C3477A60E6BBF5DBFDE1 的 JS Object 字符串

[英]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.

相关问题 将字符串转换为ajax帖子中的json对象 - Convert string to json object inside ajax post 在对JSON数据进行Ajax调用之前,请检查我的JS变量是JAVASCRIPT对象还是STRING-&gt; - Check if my JS variable is JAVASCRIPT object or STRING --> before making a ajax call for JSON data 将js Array()转换为JSon对象以与JQuery .ajax一起使用 - Convert js Array() to JSon object for use with JQuery .ajax Django / Ajax / Javasdript JSON.parse将字符串转换为JavaScript对象 - Django/Ajax/Javasdript JSON.parse convert string to JavaScript object 如何将嵌套的JSON对象转换为URL查询字符串以通过AJAX传递? - how to convert a nested JSON object to a URL query string to pass by AJAX? 卡在使用AJAX GET将json数据添加到我的表的脚本中 - Stuck with script that uses AJAX GET to add json data to my table AJAX获取对象并将json字符串数据与当前值进行比较 - AJAX to get object and compare the json string data to the current value 为什么我的ajax GET数据会产生一个奇怪的JSON对象? - why is my ajax GET data making a strange JSON object? Convert PHP array of JSON to JavaScript object in AJAX - Convert PHP array of JSON to JavaScript object in AJAX 将javascript对象或数组转换为json以获取ajax数据 - Convert javascript object or array to json for ajax data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM