用于 JSOUP Android 的 PHP JSON 数组编码

标签 php mysql arrays json

我正在尝试构建一个目录数据库,该数据库将与 Android 应用程序交互以在扫描条形码时进行查看。该应用程序应询问 MySQL 数据库并查询与零件相关的各种项目。 MySQL 部分的代码在这里:

<?php
if(isset($_POST['submit']) || isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

    $tables_result = mysql_query("SHOW TABLES") or die(mysql_error());

    while ($table = mysql_fetch_row($tables_result)) {
        $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
        if (mysql_num_rows($query_results) > 0) {
            $fields = mysql_query("SHOW COLUMNS FROM ".$table[0]);

            while ($res = mysql_fetch_array($fields)) {
                $names[] = $res[0];
            }

            $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());

            while($array = mysql_fetch_array($query_results)){
                $rows["part"] = $comments[0];
                $rows["fields"] = $names;
                for($i = 0; $i < mysql_num_fields($query_results); $i++){
                    $rows[$names[$i]] = $array[$i];
                }
                echo json_encode($rows);
            }
        }
    }
    mysql_free_result($tables_result); 
}
if(!isset($_POST['submit']) || !isset($_POST['catalog'])) {
    echo "
    <form method='post'>
    <input type='text' name='catalog' />
    <input type='submit' name='submit' />
    </form>
    ";
}
?>

但是,例如,这样的结果将是:

{"part":"Lospa Tibial Baseplate","fields":["catalog","size"],"catalog":"01.10.50B","size":"11"}

我在Android客户端使用JSOUP,它有返回JSONArray的功能。我过去使用它时的 JSONArray 看起来像这样(我遇到问题的 JSON 部分是“字段”对象):

{"part":"Lospa Tibial Baseplate","fields":{["catalog","size"]},"catalog":"01.10.50B","size":"11"}

我已经尝试在对字段数组进行编码之前直接对数组进行编码,但是没有成功并返回了一堆斜线和无用的东西:

{"part":"Lospa Tibial Baseplate","fields":"[\"catalog\",\"size\"]","catalog":"01.10.50B","size":"11"}

解决此问题的最佳方法是什么?我宁愿不只是将两个括号硬编码到那里,但如果这是最后一个选项,请告诉我如何做到这一点,因为我不能只是 $rows["fields"] = "{"。 $名字。"}";

最佳答案

尝试在构建完成后对结构进行编码:

if(isset($_POST['submit']) || isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

    $tables_result = mysql_query("SHOW TABLES") or die(mysql_error());

    while ($table = mysql_fetch_row($tables_result)) {
        $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
        if (mysql_num_rows($query_results) > 0) {
            $fields = mysql_query("SHOW COLUMNS FROM ".$table[0]);

            while ($res = mysql_fetch_array($fields)) {
                $names[] = $res[0];
            }

            $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());

            while($array = mysql_fetch_array($query_results)){
                $rows["part"] = $comments[0];
                $rows["fields"] = $names;
                for($i = 0; $i < mysql_num_fields($query_results); $i++){
                    $rows[$names[$i]] = $array[$i];
                }
            }

            // JSON encode at the end
            echo json_encode($rows);
        }
    }
    mysql_free_result($tables_result); 
}

顺便说一下,这是无效的 JSON:

{"fields":{["catalog","size"]},"catalog":"01.10.50B","size":"11"}

如果 fields 必须是一个对象,应该更像这样。

{"fields":{"key":["catalog","size"]},"catalog":"01.10.50B","size":"11"}

所以最好检查一下你的 JSOUP 解析是否设置正确。

关于用于 JSOUP Android 的 PHP JSON 数组编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712602/

相关文章:

javascript - 将新创建的 dom 元素添加到一个空的 jQuery 对象

php - 如何使用其他表中的外键删除具有主键的行?

php - 映射//Entity 和//Entity 彼此不一致 - Symfony2/Doctrine2

php - 在 PHP 中检测 base64 编码?

php - 查找用户附近的事件地点

mysql - 我可以在 Mysql 中计算两列之间的总和吗?

java - 如何使用 GCM 使用 notification_key?

PHP 电子邮件给客户

c - 确定数组中的最大值

javascript - jQuery/javascript : background-image, 滚动变化