JSONException:Value Array无法转换为JSONArray

时间:2014-07-07 19:47:17

标签: java php android arrays json

是我遇到这个问题的第二天。

我在LogCat收到了这个:

  
    

解析数据时出错org.json.JSONException:类型java.lang.String的值数组无法转换为JSONArray

  

我正在对我的数据库进行查询,并将对我来说重要的信息放在这样的数组上:

07-07 21:01:00.686: E/getpostresponse(17902):  result= Array
07-07 21:01:00.686: E/getpostresponse(17902): (
07-07 21:01:00.686: E/getpostresponse(17902):     [0] => Array
07-07 21:01:00.686: E/getpostresponse(17902):         (
07-07 21:01:00.686: E/getpostresponse(17902):             [0] => 7
07-07 21:01:00.686: E/getpostresponse(17902):             [1] => mouse
07-07 21:01:00.686: E/getpostresponse(17902):             [2] => Barcelona
07-07 21:01:00.686: E/getpostresponse(17902):             [3] => Abbey IV Ale Yeast
07-07 21:01:00.686: E/getpostresponse(17902):             [4] => 0000-00-00 00:00:00
07-07 21:01:00.686: E/getpostresponse(17902):             [5] => Ale
07-07 21:01:00.686: E/getpostresponse(17902):             [6] => 11
07-07 21:01:00.686: E/getpostresponse(17902):             [7] => 20
07-07 21:01:00.686: E/getpostresponse(17902):             [8] => prueba
07-07 21:01:00.686: E/getpostresponse(17902):             [9] => Admiral
07-07 21:01:00.686: E/getpostresponse(17902):             [10] => Ahtanum
07-07 21:01:00.686: E/getpostresponse(17902):             [11] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [12] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [13] => 80
07-07 21:01:00.686: E/getpostresponse(17902):             [14] => 20
07-07 21:01:00.686: E/getpostresponse(17902):             [15] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [16] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [17] => Amber Dry Extract
07-07 21:01:00.686: E/getpostresponse(17902):             [18] => Biscuit Malt
07-07 21:01:00.686: E/getpostresponse(17902):             [19] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):         )
07-07 21:01:00.686: E/getpostresponse(17902):     [1] => Array
07-07 21:01:00.686: E/getpostresponse(17902):         (
07-07 21:01:00.686: E/getpostresponse(17902):             [0] => 8
07-07 21:01:00.686: E/getpostresponse(17902):             [1] => mouse
07-07 21:01:00.686: E/getpostresponse(17902):             [2] => Barcelona
07-07 21:01:00.686: E/getpostresponse(17902):             [3] => Abbey IV Ale Yeast
07-07 21:01:00.686: E/getpostresponse(17902):             [4] => 0000-00-00 00:00:00
07-07 21:01:00.686: E/getpostresponse(17902):             [5] => Ale
07-07 21:01:00.686: E/getpostresponse(17902):             [6] => 50
07-07 21:01:00.686: E/getpostresponse(17902):             [7] => 8
07-07 21:01:00.686: E/getpostresponse(17902):             [8] => prueba2
07-07 21:01:00.686: E/getpostresponse(17902):             [9] => Ahtanum
07-07 21:01:00.686: E/getpostresponse(17902):             [10] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [11] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [12] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [13] => 100
07-07 21:01:00.686: E/getpostresponse(17902):             [14] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [15] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [16] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [17] => Black (Patent) Malt
07-07 21:01:00.686: E/getpostresponse(17902):             [18] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [19] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):         )
07-07 21:01:00.686: E/getpostresponse(17902):     [2] => Array
07-07 21:01:00.686: E/getpostresponse(17902):         (
07-07 21:01:00.686: E/getpostresponse(17902):             [0] => 9
07-07 21:01:00.686: E/getpostresponse(17902):             [1] => mouse
07-07 21:01:00.686: E/getpostresponse(17902):             [2] => Valencia
07-07 21:01:00.686: E/getpostresponse(17902):             [3] => Abbey Ale
07-07 21:01:00.686: E/getpostresponse(17902):             [4] => 0000-00-00 00:00:00
07-07 21:01:00.686: E/getpostresponse(17902):             [5] => Lager
07-07 21:01:00.686: E/getpostresponse(17902):             [6] => 33
07-07 21:01:00.686: E/getpostresponse(17902):             [7] => 4
07-07 21:01:00.686: E/getpostresponse(17902):             [8] => prueba3
07-07 21:01:00.686: E/getpostresponse(17902):             [9] => Admiral
07-07 21:01:00.686: E/getpostresponse(17902):             [10] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [11] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [12] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [13] => 100
07-07 21:01:00.686: E/getpostresponse(17902):             [14] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [15] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [16] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [17] => Aromatic Malt
07-07 21:01:00.686: E/getpostresponse(17902):             [18] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):             [19] => vacio
07-07 21:01:00.686: E/getpostresponse(17902):         )
07-07 21:01:00.686: E/getpostresponse(17902): )

这是我的PHP代码:

$resut = array();
$result = $db->cervezas($user);//this function returns the array
print_r($result);
//$json_data = array();
//  while($row=mysql_fetch_array($result)){
//      array_push($json_data, $row);
//  }

//$json_data = json_encode($json_data);

$json_data = json_encode($result);
echo $json_data;

我也给你了Java代码。代码在调用Asyntask之后运行:

public boolean cervezastatus(){
    ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();

    postparameters2send.add(new BasicNameValuePair("tipo","agua"));
    SharedPreferences prefe=getSharedPreferences("datos",Context.MODE_PRIVATE);
    String user = prefe.getString("user","");
    postparameters2send.add(new BasicNameValuePair("user",user));
    String URL_connect="http://"+IP_Server+"/pide_cerveza.php";

    jdata=post.getserverdata(postparameters2send, URL_connect);

    if (jdata != null) { 
       for (int i=0;i<jdata.length();i++){ 
            try {
                Cerveza cerveza2 = new Cerveza();

                cerveza2.setIdcerveza(jdata.getJSONObject(i).getInt("idcerveza"));
                cerveza2.setUsuario(jdata.getJSONObject(i).getString("usuario"));
                cerveza2.setAgua(jdata.getJSONObject(i).getString("agua"));
                cerveza2.setLevadura(jdata.getJSONObject(i).getString("levadura"));
                cerveza2.setFecha(jdata.getJSONObject(i).getString("fecha"));
                cerveza2.setTipo(jdata.getJSONObject(i).getString("tipo"));
                cerveza2.setIbu(jdata.getJSONObject(i).getInt("ibu"));
                cerveza2.setCantidad(jdata.getJSONObject(i).getDouble("cantidad"));
                cerveza2.setNombre(jdata.getJSONObject(i).getString("nombre"));
                cerveza2.setLupulo(jdata.getJSONObject(i).getString("lupulo"));
                cerveza2.setLupulo1(jdata.getJSONObject(i).getString("lupulo1"));
                cerveza2.setLupulo2(jdata.getJSONObject(i).getString("lupulo2"));
                cerveza2.setLupulo3(jdata.getJSONObject(i).getString("lupulo3"));
              cerveza2.setLupulo_porc(jdata.getJSONObject(i).getDouble("lupulo_porc"));
            cerveza2.setLupulo1_porc(jdata.getJSONObject(i).getDouble("lupulo1_porc"));
            cerveza2.setLupulo2_porc(jdata.getJSONObject(i).getDouble("lupulo2_porc"));
            cerveza2.setLupulo3_porc(jdata.getJSONObject(i).getDouble("lupulo3_porc"));
                cerveza2.setGrano(jdata.getJSONObject(i).getString("grano"));
                cerveza2.setGrano1(jdata.getJSONObject(i).getString("grano1"));
                cerveza2.setGrano2(jdata.getJSONObject(i).getString("grano2"));
                cerveza2.setGrano3(jdata.getJSONObject(i).getString("grano3"));

                cerveza_lista.add(cerveza2);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           } 
        } 
        if (jdata!=null && jdata.length() > 0){
            Log.e("no es null","dentro del if");
            JSONObject json_data; //creamos un objeto JSON

            try {
                json_data = jdata.getJSONObject(1);
                String cervezaArray=json_data.getString("Nombre");//accedemos al valor 

                Log.e("longitud", "long= "+jdata.length());
                Log.e("pruebaebeba","psaiaiaia= "+cervezaArray);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                   
                return true;
          }else{    //json obtenido invalido verificar parte WEB.
                     Log.e("JSON  ", "ERROR");
                    return false;
          }
     }

编辑:

我的logcat日志没有print_r($ result)

07-07 22:52:20.506: E/getpostresponse(17902):  result= [["7","mouse","Barcelona","Abbey IV Ale Yeast","0000-00-00 00:00:00","Ale","11","20","prueba","Admiral","Ahtanum","vacio","vacio","80","20","vacio","vacio","Amber Dry Extract","Biscuit Malt","vacio"],["8","mouse","Barcelona","Abbey IV Ale Yeast","0000-00-00 00:00:00","Ale","50","8","prueba2","Ahtanum","vacio","vacio","vacio","100","vacio","vacio","vacio","Black (Patent) Malt","vacio","vacio"],["9","mouse","Valencia","Abbey Ale","0000-00-00 00:00:00","Lager","33","4","prueba3","Admiral","vacio","vacio","vacio","100","vacio","vacio","vacio","Aromatic Malt","vacio","vacio"]]
07-07 22:52:20.506: E/DEspues de recibir jdata(17902): fail
07-07 22:52:20.516: W/System.err(17902): org.json.JSONException: Value     ["7","mouse","Barcelona","Abbey IV Ale Yeast","0000-00-00 00:00:00","Ale","11","20","prueba","Admiral","Ahtanum","vacio","vacio","80","20","vacio","vacio","Amber Dry Extract","Biscuit Malt","vacio"] at 0 of type org.json.JSONArray cannot be converted to JSONObject
07-07 22:52:20.516: W/System.err(17902):    at org.json.JSON.typeMismatch(JSON.java:100)
07-07 22:52:20.516: W/System.err(17902):    at org.json.JSONArray.getJSONObject(JSONArray.java:514)
07-07 22:52:20.516: W/System.err(17902):    at test.Droidlogin.HiScreen.cervezastatus(HiScreen.java:302)
07-07 22:52:20.516: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:200)
07-07 22:52:20.516: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:1)
07-07 22:52:20.516: W/System.err(17902):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-07 22:52:20.516: W/System.err(17902):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-07 22:52:20.516: W/System.err(17902):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-07 22:52:20.516: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-07 22:52:20.516: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-07 22:52:20.516: W/System.err(17902):    at java.lang.Thread.run(Thread.java:841)
07-07 22:52:20.516: W/System.err(17902): org.json.JSONException: Value ["8","mouse","Barcelona","Abbey IV Ale Yeast","0000-00-00 00:00:00","Ale","50","8","prueba2","Ahtanum","vacio","vacio","vacio","100","vacio","vacio","vacio","Black (Patent) Malt","vacio","vacio"] at 1 of type org.json.JSONArray cannot be converted to JSONObject
07-07 22:52:20.516: W/System.err(17902):    at org.json.JSON.typeMismatch(JSON.java:100)
07-07 22:52:20.516: W/System.err(17902):    at org.json.JSONArray.getJSONObject(JSONArray.java:514)
07-07 22:52:20.516: W/System.err(17902):    at test.Droidlogin.HiScreen.cervezastatus(HiScreen.java:302)
07-07 22:52:20.516: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:200)
07-07 22:52:20.516: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:1)
07-07 22:52:20.516: W/System.err(17902):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-07 22:52:20.516: W/System.err(17902):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-07 22:52:20.516: W/System.err(17902):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-07 22:52:20.516: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-07 22:52:20.516: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-07 22:52:20.516: W/System.err(17902):    at java.lang.Thread.run(Thread.java:841)
07-07 22:52:20.526: W/System.err(17902): org.json.JSONException: Value ["9","mouse","Valencia","Abbey Ale","0000-00-00 00:00:00","Lager","33","4","prueba3","Admiral","vacio","vacio","vacio","100","vacio","vacio","vacio","Aromatic Malt","vacio","vacio"] at 2 of type org.json.JSONArray cannot be converted to JSONObject
07-07 22:52:20.526: W/System.err(17902):    at org.json.JSON.typeMismatch(JSON.java:100)
07-07 22:52:20.526: W/System.err(17902):    at org.json.JSONArray.getJSONObject(JSONArray.java:514)
07-07 22:52:20.526: W/System.err(17902):    at test.Droidlogin.HiScreen.cervezastatus(HiScreen.java:302)
07-07 22:52:20.526: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:200)
07-07 22:52:20.526: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:1)
07-07 22:52:20.526: W/System.err(17902):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-07 22:52:20.526: W/System.err(17902):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-07 22:52:20.526: W/System.err(17902):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-07 22:52:20.526: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-07 22:52:20.526: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-07 22:52:20.526: W/System.err(17902):    at java.lang.Thread.run(Thread.java:841)
07-07 22:52:20.526: E/no es null(17902): dentro del if
07-07 22:52:20.526: W/System.err(17902): org.json.JSONException: Value ["8","mouse","Barcelona","Abbey IV Ale Yeast","0000-00-00 00:00:00","Ale","50","8","prueba2","Ahtanum","vacio","vacio","vacio","100","vacio","vacio","vacio","Black (Patent) Malt","vacio","vacio"] at 1 of type org.json.JSONArray cannot be converted to JSONObject
07-07 22:52:20.526: W/System.err(17902):    at org.json.JSON.typeMismatch(JSON.java:100)
07-07 22:52:20.526: W/System.err(17902):    at org.json.JSONArray.getJSONObject(JSONArray.java:514)
07-07 22:52:20.526: W/System.err(17902):    at test.Droidlogin.HiScreen.cervezastatus(HiScreen.java:336)
07-07 22:52:20.526: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:200)
07-07 22:52:20.526: W/System.err(17902):    at test.Droidlogin.HiScreen$asynccerveza.doInBackground(HiScreen.java:1)
07-07 22:52:20.526: W/System.err(17902):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-07 22:52:20.526: W/System.err(17902):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-07 22:52:20.526: W/System.err(17902):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-07 22:52:20.526: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-07 22:52:20.536: W/System.err(17902):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-07 22:52:20.536: W/System.err(17902):    at java.lang.Thread.run(Thread.java:841)

编辑2:我的功能

public function cervezas($user){
//Obtenemos el idusuario en funcion al nombre de usuario que nos pasan
    $iduser_aux = mysql_query("SELECT idusuario FROM usuarios WHERE username='".$user."'")or die("query failed");
    $id_user_res = mysql_fetch_row($iduser_aux);
    $iduser = $id_user_res[0];

    //Creamos vector inicializando todos los valores a "". Este será el vector a devolver al final. 
    /*
    0->idcerveza, 1->usuario, 2->agua, 3->levadura, 4->fecha, 5->tipo, 6->ibu, 7->cantidad, 8->nombre, 9->lupulo, 10->lupulo1, 11->lupulo2, 12->lupulo3, 
    13->lupulo_porc, 14->lupulo1_porc, 15->lupulo2_porc, 16->lupulo3_porc, 17->grano, 18->grano1, 19->grano2, 20->grano3
    */
    $array = array(
        array("vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio"),
        array("vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio"),
        array("vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio", "vacio"),
    );


    //Seleccionamos todas las cerverzas que ha creado un usuario. Puede ser 0,1 o muchas
    $cervezas_aux = mysql_query("SELECT c.idcerveza, (SELECT username FROM usuarios WHERE idusuario='".$iduser."') usuario, 
    (SELECT Ciudad FROM agua WHERE idagua=c.idagua) agua, (SELECT Nombre FROM levadura WHERE idlevadura=c.idlevadura) levadura, 
    c.fecha, c.tipo, c.ibu, c.cantidad, c.nombre FROM cerveza c WHERE idusuario='".$iduser."'")or die("Query failed");
    //$cervezas = mysql_fetch_array($cervezas_aux);
    $t=0;
    $i=0;
    while($row = mysql_fetch_object($cervezas_aux)){
            //echo $row->idcerveza;
            $array[$t][0] = $row->idcerveza;//Metemos idcerveza en el array
            $array[$t][1] = $row->usuario;//Metemos usuario en el array
            $array[$t][2] = $row->agua;
            $array[$t][3] = $row->levadura;//Metemos levadura en el array
            $array[$t][4] = $row->fecha;//Metemos fecha en el array
            $array[$t][5] = $row->tipo;//Metemos tipo en el array
            $array[$t][6] = $row->ibu;//Metemos ibu en el array
            $array[$t][7] = $row->cantidad;//Metemos cantidad en el array
            $array[$t][8] = $row->nombre;//Metemos nombre en el array
            //$t++;
    //  }
    //$i=0;


    //foreach((array) $cervezas_aux as $cerveza ){

        //Como solo nos falta por meter el lupulo, porcentaje y grano, debemos consultar primero cuanto hay de cada tipo
        $lupulo_cont = mysql_query("SELECT COUNT(l.idlupulo) FROM cerveza c, lupulo_has_cerveza l WHERE c.idcerveza = l.idcerveza AND l.idcerveza = '".$array[$t][0]."' ")or die("Query failed1"); 
        $count_lupulo = mysql_fetch_row($lupulo_cont);

        $grano_cont = mysql_query("SELECT COUNT(g.idgrano) FROM cerveza c, cerveza_has_grano g WHERE c.idcerveza = g.idcerveza AND g.idcerveza = '".$array[$t][0]."' ")or die("Query failed2"); 
        $count_grano = mysql_fetch_row($grano_cont);

        if($count_lupulo[0] >= 1 && $count_grano[0] >= 1){
            $lupulos = mysql_query("SELECT (SELECT Nombre FROM lupulo WHERE idlupulo = l.idlupulo) lupulo, l.porcentaje FROM cerveza c, lupulo_has_cerveza l WHERE c.idcerveza = l.idcerveza AND l.idcerveza = '".$array[$t][0]."' "); 
            $cont_l = 1;
            //foreach((array) $lupulos as $z=>$lupulo ){
            while($lu = mysql_fetch_object($lupulos)){
                if($count_lupulo[0] == 1){
                    $array[$i][9] = $lu->lupulo;
                    $array[$i][13] = $lu->porcentaje;
                }else if($count_lupulo[0] == 2){
                    if($cont_l == 1){
                        $array[$i][9] = $lu->lupulo;
                        $array[$i][13] = $lu->porcentaje;
                        $cont_l++;
                    }else{
                        $array[$i][10] = $lu->lupulo;
                        $array[$i][14] = $lu->porcentaje;
                        $cont_l = 1;
                    }
                }else if($count_lupulo[0] == 3){
                    if($cont_l == 1){
                        $array[$i][9] = $lu->lupulo;
                        $array[$i][13] = $lu->porcentaje;
                        $cont_l++;
                    }else if($cont_l == 2){
                        $array[$i][10] = $lu->lupulo;
                        $array[$i][14] = $lu->porcentaje;
                        $cont_l++;
                    }else{
                        $array[$i][11] = $lu->lupulo;
                        $array[$i][15] = $lu->porcentaje;
                        $cont_l = 1;
                    }
                }else if($count_lupulo[0] == 4){
                    if($cont_l == 1){
                        $array[$i][9] = $lu->lupulo;
                        $array[$i][13] = $lu->porcentaje;
                        $cont_l++;
                    }else if($cont_l == 2){
                        $array[$i][10] = $lu->lupulo;
                        $array[$i][14] = $lu->porcentaje;
                        $cont_l++;
                    }else if($cont_l == 3){
                        $array[$i][11] = $lu->lupulo;
                        $array[$i][15] = $lu->porcentaje;
                        $cont_l++;
                    }else{
                        $array[$i][12] = $lu->lupulo;
                        $array[$i][16] = $lu->porcentaje;
                        $cont_l = 1;
                    }
                }
            }
            $granos = mysql_query("SELECT (SELECT Nombre FROM grano WHERE idgrano = g.idgrano) grano FROM cerveza c, cerveza_has_grano g WHERE c.idcerveza = g.idcerveza AND g.idcerveza = '".$array[$t][0]."' "); 
            $cont_g = 1;
            //foreach((array) $granos as $j=>$grano ){
            while($gr = mysql_fetch_object($granos)){
                if($count_grano[0] == 1){
                    $array[$i][17] = $gr->grano;
                }else if($count_grano[0] == 2){
                    if($cont_g == 1){
                        $array[$i][17] = $gr->grano;
                        $cont_g++;
                    }else{
                        $array[$i][18] = $gr->grano;
                        $cont_g = 1;
                    }
                }else if($count_grano[0] == 3){
                    if($cont_g == 1){
                        $array[$i][17] = $gr->grano;
                        $cont_g++;
                    }else if($cont_g == 2){
                        $array[$i][18] = $gr->grano;
                        $cont_g++;
                    }else{
                        $array[$i][19] = $gr->grano;
                        $cont_g = 1;
                    }
                }else if($count_grano[0] == 4){
                    if($cont_g == 1){
                        $array[$i][17] = $gr->grano;
                        $cont_g++;
                    }else if($cont_g == 2){
                        $array[$i][18] = $gr->grano;
                        $cont_g++;
                    }else if($cont_g == 3){
                        $array[$i][19] = $gr->grano;
                        $cont_g++;
                    }else{
                        $array[$i][20] = $gr->grano;
                        $cont_g = 1;
                    }
                }
            }

        }
        $i++;
        $t++;
    }
    //print_r($array);
    return $array;
}

编辑3: 我改变了数组声明: $ array = array();而不是$ array = array(array(&#34; vacio&#34;,&#34; vacio&#34;,...)。 现在我收到了像JSON格式的结果:

result= [{"0":"7","1":"mouse","2":"Barcelona","3":"Abbey IV Ale Yeast","4":"0000-00-00 00:00:00","5":"Ale","6":"11","7":"20","8":"prueba","9":"Admiral","13":"80","10":"Ahtanum","14":"20","17":"Amber Dry Extract","18":"Biscuit Malt"},{"0":"8","1":"mouse","2":"Barcelona","3":"Abbey IV Ale Yeast","4":"0000-00-00 00:00:00","5":"Ale","6":"50","7":"8","8":"prueba2","9":"Ahtanum","13":"100","17":"Black (Patent) Malt"},{"0":"9","1":"mouse","2":"Valencia","3":"Abbey Ale","4":"0000-00-00 00:00:00","5":"Lager","6":"33","7":"4","8":"prueba3","9":"Admiral","13":"100","17":"Aromatic Malt"}]

但是因为缺少钥匙而仍然给我错误。但格式是正确的(我认为)。 如果我这样做:

$array[$t][9] = $vacio;
$array[$t][10] = $vacio;
$array[$t][11] = $vacio;
$array[$t][12] = $vacio;
$array[$t][13] = $vacio;
$array[$t][14] = $vacio;
$array[$t][15] = $vacio;
$array[$t][16] = $vacio;
$array[$t][17] = $vacio;
$array[$t][18] = $vacio;
$array[$t][19] = $vacio;
$array[$t][20] = $vacio;

数组格式再次更改为Array格式。

拜托,我需要你的帮助。我不知道我能做什么了。 谢谢。

2 个答案:

答案 0 :(得分:0)

您的JSON是一个数组数组,因此您无法访问任何对象。

如果值未配对,则几乎没用。

不确定如何获取数据,但最好构建关联数组,然后将其编码为php中的json。

因此,您可以使用mysql_fetch_assoc代替mysql_fetch_array

答案 1 :(得分:0)

您正在尝试将JSONArray类型转换为JSONObject - &gt;那不会起作用。

如果您不确定,可以测试第一个字符[(暗示JSONArray类型)或{(暗示JSONObject类型),并相应地进行解析。或者,您可以通过简单地将JSONString包装在{}之间(如果尚未包装)将JSONArray类型转换为JSONObject,以避免解析错误。

一般来说,JSON数据最常见的问题(我之前遇到的每个问题)如下:

  1. 额外(不可见)字符 - 尝试修剪数据
  2. 标题不匹配(而PHP涉及在回显结果之前添加header("Content-Type:application/json");
  3. 无效的字符集(确保您的JSON字符串符合UTF-8编码)
  4. 我希望它有所帮助。