How to parse JSON Array in Array with Volley?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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?










share|improve this question





























    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?










    share|improve this question

























      0












      0








      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?










      share|improve this question














      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?







      android android-studio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 3:45









      Selim OrhanSelim Orhan

      203




      203
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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ı






          share|improve this answer

































            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






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454995%2fhow-to-parse-json-array-in-array-with-volley%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              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ı






              share|improve this answer






























                0














                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ı






                share|improve this answer




























                  0












                  0








                  0







                  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ı






                  share|improve this answer















                  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ı







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 11:38

























                  answered Nov 24 '18 at 5:00









                  Berke AtacBerke Atac

                  1465




                  1465

























                      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






                      share|improve this answer




























                        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






                        share|improve this answer


























                          0












                          0








                          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






                          share|improve this answer













                          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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 24 '18 at 4:34









                          saeedatasaeedata

                          44137




                          44137






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454995%2fhow-to-parse-json-array-in-array-with-volley%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini