Json does not bring all the results

1

good morning I'm doing an application with Android Studio 3.0.1 with a mysql database and at the time of the query it returns me a json and at the moment of that comes to the app returns me

  

["2", "Example2", "Author2", "Editorial2", "Faculty2", "0"] ["7", "Example2", "Author4", "Editorial4", "Faculty5", " 0 "]

the response but when passing it to a JSONArray it only gives me the first part of the result

  

["2", "Example2", "Author2", "Editorial2", "Faculty2", "0"]

but I do not know what the error is because it does not give me the result as I want it, I am new programming and if you can tell me what I am doing wrong or where or how to look for information I will be grateful annex my code

    private void ConsultaTitulo(String URL) {
        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest stringRequest =  new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                try {
                    String a;
                    Log.d("Json responda",response);
                    ja = new JSONArray(response);
                    String contra = ja.getString(0);
                    Log.d("Json responda",response);

                    for (int i = 0; i < ja.length(); i++)
                    {
                        JSONObject jsonObj = ja.getJSONObject(i);
                        Log.d("Json responda",  String.valueOf(jsonObj.get("id")));

                    }

                  //  Log.d("Json responda",contra);

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

            }
        }, new Response.ErrorListener() {
            @Override

            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }

Error

  

04-11 05: 34: 48.826 4349-4349 / com.example.ferney.bibliotecacur D / Json answer:
                                                                                 ["2", "Example2", "Author2", "Editorial2", "Faculty2", "0"] ["7", "Example2", "Author4", "Editorial4", "Faculty5", "0"]   04-11 05: 34: 48.826 4349-4349 / com.example.ferney.bibliotecacur D / Json answer:
                                                                                 ["2", "Example2", "Author2", "Editorial2", "Faculty2", "0"] ["7", "Example2", "Author4", "Editorial4", "Faculty5", "0"]   04-11 05: 34: 48.870 4349-4349 / com.example.ferney.bibliotecacur W / System.err: org.json.JSONException: Value 2 at 0 of type java.lang.String can not be converted to JSONObject   04-11 05: 34: 48.872 4349-4349 / com.example.ferney.bibliotecacur W / System.err: at org.json.JSON.typeMismatch (JSON.java:100)   04-11 05: 34: 48.872 4349-4349 / com.example.ferney.bibliotecacur W / System.err: at org.json.JSONArray.getJSONObject (JSONArray.java:525)

    
asked by esteban fabian patiño 11.04.2018 в 07:37
source

1 answer

3

The error indicates that you are trying to convert information to JSONObject , but this information does not have the structure of a Json object.

  

org.json.JSONException: Value 2 at 0 of type java.lang.String can not   be converted to JSONObject

Remember that the answer .Json can be of two types:

  

- If the .json starts with { it is considered as a Json object.

     

- If the .json starts with [ it is considered as a Json fix.

In the case you mention, if you receive this as an answer:

["2","Ejemplo2","Autor2","Editorial2","Facultad2","0"]["7","Ejemplo2","Autor4","Editorial4","Facultad5","0"]

you would only get a value as JSON Array that would be the first one:

["2","Ejemplo2","Autor2","Editorial2","Facultad2","0"]

now with respect to the error, in fact there is no element that is considered JSON object, this element should be defined by the containers { and } , for this reason when you try to convert to JSONObject is generated the error JSONException .

You even try to call a key called id of the object but you have not defined keys

jsonObj.get("id")

Consider changing the structure of your answer json as an example, an array with objects and each object has keys and their respective value:

[{
    "id": "2",
    "nombre": "Ejemplo2",
    "autor": "Autor2",
    "editorial": "Editorial2",
    "facultad": "Facultad2",
    "valor": "0"
}, {
    "id": "7",
    "nombre": "Ejemplo2",
    "autor": "Autor4",
    "editorial": "Editorial4",
    "facultad": "Facultad5",
    "valor": "0"
}]
    
answered by 11.04.2018 / 20:44
source