Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Android中解释接收到的Json_Php_Android_Json - Fatal编程技术网

Php 在Android中解释接收到的Json

Php 在Android中解释接收到的Json,php,android,json,Php,Android,Json,我在这里工作,在我的应用程序中,我在android中接收到一个Json。这是我发送Json的php脚本 <?php require_once 'DB_connect.php'; class populatelist{ private $con; private $conn; function __construct() { $this->con = new DB_connect(); $this->conn =

我在这里工作,在我的应用程序中,我在android中接收到一个Json。这是我发送Json的php脚本

<?php
require_once 'DB_connect.php';
class populatelist{
    private $con;
    private $conn;

    function __construct()
    {
        $this->con = new DB_connect();
        $this->conn = $this->con->connectWithRestaurant();
    }
    function selectallfields(){
        $query = "SELECT * FROM `restaurant_time` LIMIT 50";
        $result = $this->conn->query($query);
        if($result->num_rows >0)
        {
            while($record = $result->fetch_assoc())
            {
                $response['resname'] = $record['Restaurant_name'];
                $response['restadd'] = $record['Address'];
                $response['resttime'] = $record['Waiting_time'];
                $response['images'] = $record['Logo'];
                echo json_encode($response);
            }
            echo json_encode($response);
        }
    }
}


$function = new populatelist();
$function->selectallfields();

?>

来自服务器的响应是
JSONObject
格式的。不是一个
JSONArray

修复你的php

$response = array(); // JSONArray container

while($record = $result->fetch_assoc()) {
    // Build each JSONObject with your desired key names
    $namedRecord = array();
    $namedRecord['resname'] = $record['Restaurant_name'];
    $namedRecord['restadd'] = $record['Address'];
    $namedRecord['resttime'] = $record['Waiting_time'];
    $namedRecord['images'] = $record['Logo'];

    // Insert each object into the array
    array_push($response, $namedRecord);
}

// Output the array of objects
echo json_encode($response);
您正在尝试创建
JSONObjects
JSONArray
,其格式如下

[{key:value,key1:value1,...},{anotherObjectKey:anotherObjectValue}]

来自服务器的响应是
JSONObject
格式的。不是一个
JSONArray

修复你的php

$response = array(); // JSONArray container

while($record = $result->fetch_assoc()) {
    // Build each JSONObject with your desired key names
    $namedRecord = array();
    $namedRecord['resname'] = $record['Restaurant_name'];
    $namedRecord['restadd'] = $record['Address'];
    $namedRecord['resttime'] = $record['Waiting_time'];
    $namedRecord['images'] = $record['Logo'];

    // Insert each object into the array
    array_push($response, $namedRecord);
}

// Output the array of objects
echo json_encode($response);
您正在尝试创建
JSONObjects
JSONArray
,其格式如下

[{key:value,key1:value1,...},{anotherObjectKey:anotherObjectValue}]

在日志中打印JSON响应并在..上进行验证

您的响应不是有效的JSON,它包含许多JSONObject,这些JSONObject应该是逗号分隔的,而不是逗号分隔的,并且所有这些JSONObject都应该包含在JSONArray中。请把它格式化好

查看此链接,了解如何在php中构建JSONArray

你的回答应该是

"rstaurants" :[
{
"resname": "Sankalp",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "25"
},
{
"resname": "South Cafe",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "20"
},
{
"resname": "Uncle Sam",
"restadd": "Infocity",
"resttime": "Pizza",
"images": "15"
},
{
"resname": "Dangee Dums",
"restadd": "Infocity",
"resttime": "Dessert",
"images": "10"
},
{
"resname": "Fresh Roast",
"restadd": "Infocity",
"resttime": "Cafe",
"images": "5"
}
]
现在是,

{
"resname": "Sankalp",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "25"
}
{
"resname": "South Cafe",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "20"
}
{
"resname": "Uncle Sam",
"restadd": "Infocity",
"resttime": "Pizza",
"images": "15"
}
{
"resname": "Dangee Dums",
"restadd": "Infocity",
"resttime": "Dessert",
"images": "10"
}
{
"resname": "Fresh Roast",
"restadd": "Infocity",
"resttime": "Cafe",
"images": "5"
}
让我知道它是否适合你。。。
并将其标记为答案,以便对其他人有用…

在日志中打印您的JSON响应,并在

$response = array();

while($record = $result->fetch_assoc()) 
{
    $restaurant = array();
    $restaurant['resname'] = $record['Restaurant_name'];
    $restaurant['restadd'] = $record['Address'];
    $restaurant['resttime'] = $record['Waiting_time'];
    $restaurant['images'] = $record['Logo'];
    $response[] = $restaurant;
}
echo json_encode($response);

您的响应不是有效的JSON,它包含许多JSONObject,这些JSONObject应该是逗号分隔的,而不是逗号分隔的,并且所有这些JSONObject都应该包含在JSONArray中。请把它格式化好

查看此链接,了解如何在php中构建JSONArray

你的回答应该是

"rstaurants" :[
{
"resname": "Sankalp",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "25"
},
{
"resname": "South Cafe",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "20"
},
{
"resname": "Uncle Sam",
"restadd": "Infocity",
"resttime": "Pizza",
"images": "15"
},
{
"resname": "Dangee Dums",
"restadd": "Infocity",
"resttime": "Dessert",
"images": "10"
},
{
"resname": "Fresh Roast",
"restadd": "Infocity",
"resttime": "Cafe",
"images": "5"
}
]
现在是,

{
"resname": "Sankalp",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "25"
}
{
"resname": "South Cafe",
"restadd": "Infocity",
"resttime": "South Indian",
"images": "20"
}
{
"resname": "Uncle Sam",
"restadd": "Infocity",
"resttime": "Pizza",
"images": "15"
}
{
"resname": "Dangee Dums",
"restadd": "Infocity",
"resttime": "Dessert",
"images": "10"
}
{
"resname": "Fresh Roast",
"restadd": "Infocity",
"resttime": "Cafe",
"images": "5"
}
让我知道它是否适合你。。。
并将其标记为答案,以便对其他人有用…

m很抱歉,当时我没有初始化数组,因此它显示为空。但现在初始化后,它显示“[]”。为了补充您的答案,$response不应该是第一个参数吗?array_push的第一个参数是目的地。二是源头。尝试在“我的编辑”中初始化这两个数组。这可能会解决问题(我是一个php noob),但我完全掌握了您在生产代码中所做的一切,并且运行良好。@Milind删除if语句的主体,然后复制粘贴我的代码块。很抱歉,当时我没有初始化数组,所以它显示为空。但现在初始化后,它显示“[]”。为了补充您的答案,$response不应该是第一个参数吗?array_push的第一个参数是目的地。二是源头。尝试在“我的编辑”中初始化这两个数组。这可能会解决问题(我是一个php noob),但我完全掌握了您在生产代码中所做的一切,并且运行良好。@Milind删除if语句的主体,然后复制粘贴我的代码块。它会起作用的。你能指导我吗?@Milind复制粘贴我的答案;你能给我指引吗?@Milind复制粘贴我的答案;PIt在有或没有该行的情况下都是无效的JSON。这不是问题。为什么被接受的答案是我的一个复制粘贴-在我调用它后,它有一个变量被重命名。。。。人们都很好,不管有没有这一行,JSON都是无效的。这不是问题。为什么被接受的答案是我的一个复制粘贴-在我调用它后,它有一个变量被重命名。。。。人是好的
$response = array();

while($record = $result->fetch_assoc()) 
{
    $restaurant = array();
    $restaurant['resname'] = $record['Restaurant_name'];
    $restaurant['restadd'] = $record['Address'];
    $restaurant['resttime'] = $record['Waiting_time'];
    $restaurant['images'] = $record['Logo'];
    $response[] = $restaurant;
}
echo json_encode($response);