1

I got a problem with "[ ]", I want retrieve all posts in Android:

I got in the response:

{"data":[["-70% sur tout","Et oui vous ne r\u00eavez pas...",1],["aaaaa","aaaaa",2],["bbbbb","bbbb",3]]}

But I need it for it to work

{"data":[{"-70% sur tout","Et oui vous ne r\u00eavez pas...",1},{"aaaaa","aaaaa",2},{"bbbbb","bbbb",3}]}

The error :

Value ["-70% sur tout","Et oui vous ne rêvez pas...",1] at 0 of type org.json.JSONArray cannot be converted to JSONObject

I don't know how to replace [ ] by { }.

JSON_FORCE_OBJECT don't work :

{"data":{"0":{"0":"-70% sur tout","1":"Et oui vous ne r\u00eavez pas...","2":1},"1":{"0":"aaaaa","1":"aaaaa","2":2},"2":{"0":"bbbbb","1":"bbbb","2":3}}}

The error of JSON_FORCE_OBJECT :

Value {"0":{"0":"-70% sur tout","1":"Et oui vous ne rêvez pas...","2":1},"1":{"0":"aaaaa","1":"aaaaa","2":2},"2":{"0":"bbbbb","1":"bbbb","2":3}} at data of type org.json.JSONObject cannot be converted to JSONArray

Android Studio code:

private void DisplayOffer() {
        String uRl = "http://10.0.2.2/Test/allRecentOfferProfilPage.php?businessID="+ BusinessModel.getTheBusinessID();
        StringRequest stringRequest=new StringRequest(Request.Method.GET,uRl,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    System.out.println(response);
                    JSONObject jsonObject=new JSONObject(response);
                    JSONArray array=jsonObject.getJSONArray("data");

                    for (int i=0; i<array.length(); i++){
                        JSONObject ob=array.getJSONObject(i);
                        PostModel user= new PostModel(ob.getString("title"), ob.getString("description"), ob.getInt("postID"));
                        post_data.add(user);
                    }
                    recyclerView.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        RequestQueue requestQueue= Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);
    }

Here my PHP code:

<?php

require "conn.php";

if($conn){
    
    $sql = $conn->prepare("select posts.title, posts.description, posts.postID from posts inner join business on posts.businessID=business.businessID where business.businessID= ?");
    $sql->bind_param("s", $_GET["businessID"]);
    $sql->execute();
    $data["data"]= $sql->get_result()->fetch_all();
    
    header('Content-Type:Application/json');
 
    echo json_encode($data);
    

}

?>
1
  • 1
    You are mixing JSONObject and JSONArray. {} is for object, and [] is for array. Use the appropriate one. When you JSON_FORCE_OBJECT then everything is an object so replace your usage of JSONArray with JSONObject Commented Mar 11, 2021 at 16:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.