How can I get all the data from a JSON on Android?

2

I'm doing an android application which queries a database to get the name of several sports. The query I do is the following:

if($resultset=getSQLResultSet("SELECT etiqueta_nombre from sdo_v_etiquetas_espacios GROUP BY etiqueta_nombre")){
     while ($row = $resultset->fetch_array(MYSQLI_NUM)){
        echo json_encode($row);
     }
}

And it returns the following result: ["Futbol"]["Tenis"] . The code that implements the data extraction after the query that I have done is:

protected void onPostExecute(String result) {
        JSONArray ja = null;
        try {
            ja = new JSONArray(result);
            if(!ja.isNull(0)){
                for(int i=0;i<ja.length();i++)
                    etiquetas.add(ja.getString(i).toString());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

The problem is that it only returns the first one and if I try to put a for with the length of ja.length() it returns a OutOfIndex .

    
asked by Ivan 27.04.2017 в 13:16
source

2 answers

0

The format of that JSON is not valid, in correct format it should be something like this:

["Futbol", "Tenis"]

For future reference, you can use this validator to check the format of your JSON.

With a format like the one I mentioned, the processing would be as simple as:

protected void onPostExecute(String result) {

    JSONArray ja = new JSONArray(result);

    for(int i = 0; i < ja.length(); i++)
        etiquetas.add(ja.getString(i));

}
    
answered by 27.04.2017 в 16:10
0

The problem is that you are printing your JSON in every iteration (in PHP),

 $result = array();
if($resultset=getSQLResultSet("SELECT etiqueta_nombre from sdo_v_etiquetas_espacios GROUP BY etiqueta_nombre")){
 while ($row = $resultset->fetch_array(MYSQLI_NUM)){
    $result[]=$row;
 }
}

echo json_encode($result)
    
answered by 04.12.2018 в 14:41