【问题标题】:PHP array made of sql rows to json_encode由 sql 行组成的 PHP 数组到 json_encode
【发布时间】:2015-09-14 23:42:10
【问题描述】:

我无法使用 json_encode 将由 sql 查询生成的 php 数组转换为 JSONObject。我正在使用 google volley 来实现连接。

当涉及到单行结果时,我没有遇到任何问题,但是当有超过 1 行时,我的应用程序中出现错误,这意味着我没有真正收到 JSONObject。

这是我的php代码

if (mysql_num_rows($result) > 0) {

$rutina = array();
while($row = mysql_fetch_assoc($result))
{
    $rutina[] = $row;
}}  

我就这样退货了

echo json_encode($rutina);

我知道 mysql 已被弃用,我很快就会迁移到 mysqli。

将我的 sql 行数组转换为 JSONObject 的正确方法是什么?

编辑:

这是我等待 JSONObject 的 android 代码:

JsonObjectRequest solicitudRutina = new JsonObjectRequest(
            Request.Method.POST, //metodo de solicitud
            linkrutina, //url, se cambia en las variables
            map,//el objeto JSON que contiene el usuario que intentaremos descargar
            new Response.Listener<JSONObject>() { //el listener de la respuesta
                @Override
                public void onResponse(JSONObject response) { // si existe respuesta aca se cacha,

                    String temp= response.optString("sinexito");//sinexito tiene el mensaje de error de no encontrar el usuario

                    if(temp.equals("")){//si la rutina existe, iniciamos descarga

                        rutinaview.setText(response.toString());
                        //obtenerRutina(response);
                    }
                    else{
                        Context context = getApplicationContext();
                        CharSequence text = "Problema al descargar la rutina, posiblemente no exita una asignada";
                        int duration = Toast.LENGTH_LONG;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            {
                Context context = getApplicationContext();
                CharSequence text = "Error con la base de datos.";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        }
    });
    VolleyApplication.getsInstance().getmRequestQueue().add(solicitudRutina);

我收到响应错误的祝酒词。我假设这是因为我没有得到 JSONObject?它只适用于 1 行。

【问题讨论】:

  • 其他显示json编码数据的代码在哪里?
  • 那是什么错误?
  • 您的代码将生成一个 json 数组(可能是对象字面量数组)[ {...},{...}, ...]。如果你想要一个 json-object 作为最顶层的元素,你必须使用不同于 0,1,2,3,... 的键用于 $rutina
  • @aldrin27 代码在一个请求 JSONObject 的 android 应用程序中,尽管 volley 生病了编辑我最初的问题。
  • 当你使用 json_encode 时。它将返回到一个 json 字符串,所以当它通过你的 android 时,将该 json 变成一个对象

标签: php mysql arrays json mysqli


【解决方案1】:

一般我使用这样的方式来成功解析 JSON 对象。需要非常清楚地说明页面标题必须具有 json 作为 MIME 类型,以便任何其他代码都可以轻松识别它。

<?php
header('Content-Type:application/json');
//Your Database query here...
$output = mysqli_fetch_all($rutina,MYSQLI_ASSOC);
echo json_encode($output);

它一直对我有用...无需使用 while 循环,它将输出作为数据库查询找到的行的关联数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2012-07-28
    • 2011-05-13
    • 2013-02-04
    相关资源
    最近更新 更多