如何在android中获得两个数组JSON响应

时间:2022-11-26 12:15:42

I don't know how to get my JSON response in android.

我不知道如何在android中获取我的JSON响应。

JSON response:

JSON响应:

{
  "client_id": ["1", "3", "4", "4"],
  "payed": ["0", "1", "1", "1"]
}

P.S: payed is a boolean.

P.S:支付是一个布尔值。

PHP file:

PHP文件:

$statement = mysqli_query($con, "SELECT client_id,Payed FROM reservations WHERE resto_id = '$restaurantID'");

$response = array();

while($row = mysqli_fetch_assoc($statement)){
    $response["client_id"][]=$row["client_id"];
    $response["payed"][]=$row["Payed"];
}         

echo json_encode($response);

I tried to get the response like this, but it doesn't work:

我试图得到这样的响应,但它不起作用:

Response.Listener<String> responseListener = new Response.Listener<String>() {
    public void onResponse(String response) {
    System.out.println(response);
    try {
        JSONArray jsonResponse = new JSONArray(response);
        System.out.println("JSON LENGTH =" + jsonResponse.length());

        String[] clients = new String[jsonResponse.length()];
        Boolean[] status = new Boolean[jsonResponse.length()];

        for (int i = 0; i < jsonResponse.length(); i++) {
            JSONObject obj = jsonResponse.getJSONObject(i);
            clients[i] = obj.getString("client_id");
            status[i] = obj.getBoolean("payed");
        }
    }
}

1 个解决方案

#1


0  

I actually figure it out, i just send my data in my PHP file like this:

我实际上弄清楚了,我只是在我的PHP文件中发送我的数据,如下所示:

$statement = mysqli_query($con, "SELECT client_id,Payed FROM reservations WHERE resto_id = '$restaurantID'");

$response = array();


while($row = mysqli_fetch_assoc($statement)){
    $response[]=$row;
            $response[]=$row;
}         


echo json_encode($response);

And i get the data from android like this:

我从android获取数据如下:

Response.Listener<String> responseListener = new Response.Listener<String>() {
                public void onResponse(String response) {
                    System.out.println(response);
                    try {
                        JSONArray jsonResponse = new JSONArray(response);
                        System.out.println("JSON LENGTH =" + jsonResponse.length());

                        String[] clients = new String[jsonResponse.length()];
                        String[] status = new String[jsonResponse.length()];

                        for (int i = 0; i < jsonResponse.length(); i++) {

                            JSONObject obj = jsonResponse.getJSONObject(i);
                            clients[i] = obj.getString("client_id");
                            status[i] = obj.getString("Payed");
                        }

I'll be glad if it could help someone.

如果它可以帮助某人,我会很高兴的。

#1


0  

I actually figure it out, i just send my data in my PHP file like this:

我实际上弄清楚了,我只是在我的PHP文件中发送我的数据,如下所示:

$statement = mysqli_query($con, "SELECT client_id,Payed FROM reservations WHERE resto_id = '$restaurantID'");

$response = array();


while($row = mysqli_fetch_assoc($statement)){
    $response[]=$row;
            $response[]=$row;
}         


echo json_encode($response);

And i get the data from android like this:

我从android获取数据如下:

Response.Listener<String> responseListener = new Response.Listener<String>() {
                public void onResponse(String response) {
                    System.out.println(response);
                    try {
                        JSONArray jsonResponse = new JSONArray(response);
                        System.out.println("JSON LENGTH =" + jsonResponse.length());

                        String[] clients = new String[jsonResponse.length()];
                        String[] status = new String[jsonResponse.length()];

                        for (int i = 0; i < jsonResponse.length(); i++) {

                            JSONObject obj = jsonResponse.getJSONObject(i);
                            clients[i] = obj.getString("client_id");
                            status[i] = obj.getString("Payed");
                        }

I'll be glad if it could help someone.

如果它可以帮助某人,我会很高兴的。