我不知道如何在android中获取我的JSON响应。
JSON响应:
{
"client_id": ["1", "3", "4", "4"],
"payed": ["0", "1", "1", "1"]
}
P.S:支付是一个布尔值。
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);
我试图得到这样的回复,但它不起作用:
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");
}
}
}
答案 0 :(得分:0)
我实际上是想通了,我只是在我的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);
我从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");
}
如果它可以帮助某人,我会很高兴。