是我第二天遇到此問題。JSONException:Value數組無法轉換爲JSONArray
我收到此在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($結果)
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(); 。而不是$陣列=陣列(陣列(「vacio」,「vacio」,...) 現在我收到像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格式
請,我需要你們的幫助,我不知道是什麼我可以嗎? 謝謝。
您的'logcat'日誌顯示一個PHP,爆炸的數組變量,PHP代碼有問題。 –
我的logcat日誌顯示爆炸數組,因爲我把print_r($ result);如果我評論這行不會出現。 – user3480270
請刪除'print_r($ result);'行,我想查看您獲得的JSON數據,如果您需要使用一些字符串替代來保護專有數據.. –