获取SQL结果-无法将JSONarray转换为JSONobject


Get SQL Results - JSONarray cannot be converted to JSONobject

我已经修改了几个小时的代码,我似乎无法解析android中的值并在php中使用它,我总是觉得JSONarray无法转换为JSONobject。

<?php
include 'config.php';
$con=mysql_connect("$servername", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");
if( isset($_POST["json"]) ) {
     $value = $_POST["json"]; 
     $decoded_value = json_decode($value);
    $sql = "SELECT * from table WHERE Code = '".$decoded_value->{'msg'}."'";
    $result = mysql_query($sql);
    $json = array();
    if(mysql_num_rows($result)){
        while($row=mysql_fetch_assoc($result)){
        $json['myarray'][]=$row;
        }
    }
mysql_close($con);
echo json_encode($json); 
}
?> 
json_encode($json, JSON_FORCE_OBJECT)

json_encode函数有多个参数,您可以通过这些参数来修改编码方法(请在此处查看)。在您的实例中,可以选择强制编码为对象形式,而不是数组或对象的默认形式。