0

I'm trying to parse json from my website.

JSON: https://www.yemeklerimiz.com/?json=get_category_posts&id=6

There is 2 array in json. I can not parse posts array because before coming category array.

My Volley:

 public void getPosts() {
    String url = Constant.baseUrl+"?json=get_category_posts&id="+getIntent().getStringExtra("CAT_ID");
    StringRequest request = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray  datas = jsonObject.getJSONArray("0");
                for (int i = 0; i < datas.length(); i++) {
                    PostsModel postsModel = new PostsModel();
                    JSONObject jo = datas.getJSONObject(i);
                    String id = jo.getString("id");
                    String url = jo.getString("url");
                    String title = jo.getString("title");
                    Log.d("IMAGEUR", url);
                    postsModel.setID(id);
                    postsModel.setImageURL(url);
                    postsModel.setTitle(title);
                    postsModelArrayList.add(postsModel);
                    postsAdapter.notifyDataSetChanged();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    requestQueue.add(request);
}

This not work. What can I do to get JSONObjects which in posts array?

2 Answers 2

1

While I agree with saeedata's answer, I would like to explain you how you could get this code working in its current state and without Retrofit. I think it will better help you understand how JSON works.

So what you have here is: The main JSON Object which is your response, and we can consider this the root. Inside this "response" JSON Object, you have fields such as "count" and "pages", and also another field that is a JSON Array called "posts". This "posts" field contains various other JSON Objects in itself.

Following code snippet shows how to retrieve the posts objects and retrieve the fields in it.

     JSONObject responseJSON = new JSONObject(response);
     // Retrieve the posts JSON array from the response
     JSONArray postsArray = jsonObject.getJSONArray("posts");

     for (int i = 0; i < datas.length(); i++) { //loop to iterate in JSON array
            //retrieve the single postObject in array
            JSONObject postObject = postsArray.getJSONObject(i);

            //get fields from the postObject
            String id = postObject.getString("id");
            String url = postObject.getString("url");
            String title = postObject.getString("title");
            Log.d("Title for " + i.toString(), title);
     }

The output will be the following:

Title for 0: Unsuz Şekersiz Cheesecake
Title for 1: Hurmalı Şekersiz Browni
Title for 2: Kırmızı Meyveli Pratik Cheesecake
Title for 3: Tropikal Blondie
Title for 4: Glutensiz Şekersiz Çikolatalı Muzlu Kek
Title for 5: Starbucks Havuçlu Kek
Title for 6: Çikolatalı Dondurma
Title for 7: Saray Helvası

Sign up to request clarification or add additional context in comments.

Comments

0

that's very simple with JSON tools like GSON or LoganSquare , ... . you must first create a model that fields have annotations then create a builder and finally convert your raw JSON string to model,

you can see an example in this link ;

I suggest use retrofit instead Volley because is very simple and faster than Volley

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.