Error reading Json on Android

0

I'm trying to read this Json that contains fixes, but it gives me the following error, I'm reading it with JSONArray, I tried to traverse it with a for but it does not let me extract the objects inside the json. Does anyone know what I can do or what am I doing wrong?

12-05 09:56:59.051 16157-16157/pubeat.app.version1_0 W/System.err: org.json.JSONException: Value {"Cerca de ti":{"nombre":"Cerca de ti","promos":[{"prom_id":"21","neg_id":"3","prom_desc":"prmo especial","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=21","neg_nombre":"Alitas Wings Consti","vip":"0","calificacion":"4","distancia":"0"},{"prom_id":"9","neg_id":"3","prom_desc":"chelas 2x1 todos los viernes","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=9","neg_nombre":"Alitas Wings Consti","vip":"0","calificacion":"4","distancia":"0"},{"prom_id":"18","neg_id":"12","prom_desc":"promo para ffff","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=18","neg_nombre":"fffffff","vip":"0","calificacion":null,"distancia":"0"}]},"Recomendadas":{"nombre":"Recomendadas","promos":[]},"Mas Populares":{"nombre":"Mas Populares","promos":[{"prom_id":"15","neg_id":"9","prom_desc":"promociones por hora","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=15","neg_nombre":"Wings Tesistan","vip":"0","calificacion":"5","distancia":null},{"prom_id":"21","neg_id":"3","prom_desc":"prmo especial","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=21","neg_nombre":"Alitas Wings Consti","vip":"0","calificacion":"4","distancia":"0"},{"prom_id":"9","neg_id":"3","prom_desc":"chelas 2x1 todos los viernes","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=9","neg_nombre":"Alitas Wings Consti","vip":"0","calificacion":"4","distancia":"0"}]},"Promos Decembrinas":{"nombre":"Promos Decembrinas","promos":[{"prom_id":"22","neg_id":"9","prom_desc":"programa tus posadas para este año","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=22","neg_nombre":"Wings Tesistan","vip":"0","calificacion":"5","distancia":null},{"prom_id":"22","neg_id":"9","prom_desc":"programa tus posadas para este año","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=22","neg_nombre":"Wings Tesistan","vip":"0","calificacion":null,"distancia":null}]},"Promociones Hallowen":{"nombre":"Promociones Hallowen","promos":[{"prom_id":"20","neg_id":"9","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"Wings Tesistan","vip":"0","calificacion":null,"distancia":null},{"prom_id":"20","neg_id":"66","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"minegocio58","vip":"0","calificacion":null,"distancia":"0"},{"prom_id":"20","neg_id":"101","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"minegocio93","vip":"0","calificacion":null,"distancia":"0"},{"prom_id":"20","neg_id":"10","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"Wings Army ","vip":"0","calificacion":"5","distancia":"0"},{"prom_id":"20","neg_id":"46","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"minegocio38","vip":"0","calificacion":null,"distancia":"0"},{"prom_id":"20","neg_id":"81","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"minegocio73","vip":"0","calificacion":null,"distancia":"0"},{"prom_id":"20","neg_id":"26","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"minegocio18","vip":"0","calificacion":null,"distancia":"0"},{"prom_id":"20","neg_id":"61","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"minegocio53","vip":"0","calificacion":null,"distancia":"0"},{"prom_id":"20","neg_id":"96","prom_desc":"promo de hallowen","logo":"https:\/\/pubeat.com.mx\/pubeat\/system\/ajax\/Logo.php?prom_id=20","neg_nombre":"minegocio88","vip":"0","calificacion":null,"distancia":"0"},{"prom_id":"20","neg_id":"131","prom_desc":"promo de hallowen","
12-05 09:56:59.051 16157-16157/pubeat.app.version1_0 W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
12-05 09:56:59.051 16157-16157/pubeat.app.version1_0 W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
12-05 09:56:59.051 16157-16157/pubeat.app.version1_0 W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
12-05 09:56:59.051 16157-16157/pubeat.app.version1_0 W/System.err:     at pubeat.app.version1_0.PromosCardViewFragment$1.onResponse(PromosCardViewFragment.java:59)
12-05 09:56:59.051 16157-16157/pubeat.app.version1_0 W/System.err:     at pubeat.app.version1_0.PromosCardViewFragment$1.onResponse(PromosCardViewFragment.java:52)

The code is:

public class PromosCardViewFragment extends Fragment {
    ArrayList<FuentePromosInfo> listaFuenteInfo;
    String imgUrl = "http://www.teorema.com.mx/wp-content/uploads/agua-vital.jpg";
    String imfUrl2 = "http://arribaelcampo.com.mx/wp-content/uploads/2017/03/agua-1.jpg";

    public PromosCardViewFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //Creamos las variables en donde se guardará el url de la imagen obtenida del json


        //Creamos arreglo para poblar CardViews de promos con las imagenes
        listaFuenteInfo = new ArrayList<FuentePromosInfo>();



        Response.Listener<String> promosListener = new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                try {

                    JSONArray promJson = new JSONArray(response);
                    Toast.makeText(getContext(), (CharSequence) promJson, Toast.LENGTH_SHORT).show();


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

            }

        };
        RecyclerView contenedor = (RecyclerView) view.findViewById(R.id.recyclerContenedor);
        contenedor.setHasFixedSize(true);
        LinearLayoutManager layout = new LinearLayoutManager(getContext());
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        contenedor.setAdapter(new PromosCardViewAdapter(listaFuenteInfo));
        contenedor.setLayoutManager(layout);


        PromosInfoRequest promosInfoRequest = new PromosInfoRequest(promosListener);
        RequestQueue queue = Volley.newRequestQueue(getContext());
        queue.add(promosInfoRequest);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_promos_card_view, container, false);


    }
}
    
asked by Andressualu 05.12.2017 в 17:14
source

1 answer

0

What happens is that your response is not a JSONArray, what you are receiving is a JSONObject, therefore the error when initializing the JSONArray, if what you expect is a JSONArray as such, I recommend changing the structure of your answer in JSON, meanwhile here I leave you a solution to your problem so that from your answer you can get a JSONArray, this code is already tested with the response that you put in this publication

String response = ""; //TU RESPONSE
    try {
        JSONObject jsonObject = new JSONObject(response);
        Iterator<String> iterator = jsonObject.keys();
        JSONArray jsonArray = new JSONArray();
        while (iterator.hasNext()) {
            String key = iterator.next();
            JSONObject object = new JSONObject(jsonObject.get(key).toString());
            jsonArray.put(object);
        }
        Log.d("TAG", jsonArray.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

If you have any errors, or any questions, do not hesitate to answer, greetings.

This is an example of how you can navigate your parent array and the array of promos of each item in your parent array.

        //Iterar JSONArray
        for (int i = 0; i < jsonArray.length(); i ++){
            JSONObject item = jsonArray.getJSONObject(i);
            String nombre = item.getString("nombre");
            JSONArray jsonArrayPromos = item.getJSONArray("promos");
            //Iterar JSONArrayPromos
            for (int j = 0; j < jsonArrayPromos.length(); j ++) {
                JSONObject itemPromo = jsonArrayPromos.getJSONObject(j);
                //Ejemplo de como extrar cada propiedad
                String prom_id = itemPromo.getString("prom_id");
                String neg_id = itemPromo.getString("neg_id");
               //...
                Log.d("TAG",  i + " " + itemPromo.toString());
            }
        }
    
answered by 05.12.2017 / 17:51
source