【问题标题】:Getting [object Object] after calling php array with JSON使用 JSON 调用 php 数组后获取 [object Object]
【发布时间】:2019-10-08 06:11:35
【问题描述】:

我正在尝试获取json_encoded 的数组。该数组还包含字典。举个例子,它看起来像这样 [{key:'value'}] 有 5 个键,每一行都有相同的键但值不同。字典中有大约 70000 项,因为它们是从文件中读取的,并放入 for while 循环以将它们放入数组中。

while($row = $result->fetch_assoc())
{
    $tmp[] = $row;
}
$test = json_encode($tmp);

数组和字典是从MySql 表中提取的,并且键与列相关联。我的文件名为 ipv4.phpjsIPv4.php 所以数组和dicti被定义并放入ipv4中的一个变量中,并被调用到jsIPv4中的javascript代码,我尝试将它存储到一个变量中并用JSON.parse解析它。但在我这样做之前,我至少需要打印出数组中的值。

这就是我调用php 变量的方式。

<script type="text/javascript">
    var test = <?php echo $test ?>;
    document.write(test);
</script>

如果我把它放在 '' 中,它会列出我需要的所有内容,但是当我尝试将其称为索引时:test[0] 我将把[ 作为 1. 值,所以我猜它会强制转换变成一个字符串。

【问题讨论】:

  • test 可能是一个对象,当 JavaScript 尝试将其转换为 string 时,该对象将输出为 [object Object]。尝试先调用JSON.stringify(test) 将其转换为 JSON 字符串。
  • test 是一个数组。 document.write() 的参数必须是字符串。尝试改用console.log()
  • 嗯,我没有得到 1 [object Object] 我得到 403。这就是过滤器给出的数量。当我将测试放入''时,它将显示我想要的值。但它给了我一个完整的字符串。我只需要里面的数组和dicti。
  • 你想要所有的json值返回并打印在一个表中

标签: javascript php arrays dictionary


【解决方案1】:

通过 jquery ajax 传递它会很棒。根据您的要求,我为您重新编写了一个很好的插图。

假设您的表格有列id、用户名、姓名、电子邮件等......或者您可以按如下方式创建表格并插入其中 用于测试

create table users_test(id int primary key auto_increment,username varchar(30),name varchar(30), email varchar(30));

插入测试

insert into users_test(username,name,email) values('nancy','nancy moore','nany@gmail.com');
insert into users_test(username,name,email) values('tony','tony moore','tony@gmail.com');

index.html

<!doctype html>
<html>
    <head>

   <script 
  src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> 
  type="text/javascript" charset="utf-8"></script>

        <script type="text/javascript">

$(document).ready(function(){
    $.ajax({
        url: 'test.php',
        type: 'get',
        dataType: 'JSON',
        success: function(response){
            var len = response.length;
            for(var i=0; i<len; i++){
                var id = response[i].id;
                var username = response[i].username;
                var name = response[i].name;
                var email = response[i].email;

                var tr_str = "<tr>" +
                    "<td align='center'>" + (i+1) + "</td>" +
                    "<td align='center'>" + username + "</td>" +
                    "<td align='center'>" + name + "</td>" +
                    "<td align='center'>" + email + "</td>" +
                    "</tr>";

                $("#userTable tbody").append(tr_str);
            }

        }

    });

});


</script>

    </head>
    <body>
        <div class="container">
            <table id="userTable" border="1" >
                <thead>
                    <tr>
                        <th width="5%">S.no</th>
                        <th width="20%">Username</th>
                        <th width="20%">Name</th>
                        <th width="30%">Email</th>

                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </body>
</html>

test.php

<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "you db here"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection to db failed ");
}

$test = array();

$query = "SELECT * FROM users_test ORDER BY id";

$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result)){
    $id = $row['id'];
    $username = $row['username'];
    $name = $row['name'];
    $email = $row['email'];

    $test[] = array("id" => $id,
                    "username" => $username,
                    "name" => $name,
                    "email" => $email);
}

// Encoding array in JSON format
echo json_encode($test);

【讨论】:

    【解决方案2】:

    好吧,我尝试了这段代码并使用了价值

    <?php
    while($row = $result->fetch_assoc())
    {
        $tmp[] = $row;
    } 
    $test = json_encode($tmp);
    ?>
    <script type="text/javascript">
        var test = <?php echo $test; ?>;
        document.write(JSON.stringify(test));
    </script> 
    
    My data output : [{"id":"1","name":"ranbir","city":"delhi","age":"35"},{"id":"2","name":"sonu","city":"punjab","age":"40"},{"id":"3","name":"ranbir","city":"delhi","age":"35"},{"id":"4","name":"sonu","city":"punjab","age":"40"}]
    

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      相关资源
      最近更新 更多