1

I want parse full data including Episodes, Genre Array but I am using volley lib and getting Main Objects from "anime" array!

How did I get Episodes array's data too? Any simple method?

Also I am adding those data in a single model data so please give me the understanding code.

This is my json data getting from online:

{
  "anime": [
    {
      "title": "Master",
      "img": "img.png",
      "synopsis": "Plot Summary:plot",
      "genres": [
        "adventure",
        "drama",
        "historical",
        "mystery",
        "seinen",
        "slice-of-life"
      ],
      "released": 1998,
      "status": "Completed",
      "otherName": "Master",
      "totalEpisodes": 39,
      "episodes": [
        {
          "id": "episode-1"
        },
        {
          "id": "episode-2"
        },
        {
          "id": "episode-3"
        },
        {
          "id": "episode-4"
        },
        {
          "id": "episode-5"
        },
        {
          "id": "episode-39"
        }
      ]
    },
    {
      "title": "Hyper Police",
      "img": "police.png",
      "synopsis": "Plot Summary: plot",
      "genres": [
        "action",
        "comedy",
        "police",
        "romance",
        "sci-fi"
      ],
      "released": 1997,
      "status": "Completed",
      "otherName": " ploci",
      "totalEpisodes": 25,
      "episodes": [
        {
          "id": "episode-1"
        },
        {
          "id": "episode-2"
        },
        {
          "id": "episode-3"
        },
        {
          "id": "episode-25"
        }
      ]
    }
  ]
}

This code I am calling from MainActivity but doesn’t work.


String url = "API URL";
    final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response)
        {
            try
            {
            JSONArray jsonArray = response.getJSONArray("anime");

            for (int i = 0; i < jsonArray.length(); i++)
            {
                JSONObject hit = jsonArray.getJSONObject(i);

                JSONArray data = hit.getJSONArray("episodes");

                // Extract JSONObject inside JSONObject
                JSONObject IDOb = data.getJSONObject(i);
                String ids = IDOb.getString("id");

                String title = hit.getString("title");
                String img = hit.getString("img");
                String synopsis = hit.getString("synopsis");
                        String genre = hit.getString("genres");
                        String release = hit.getString("released");
                        String status = hit.getString("status");
                        String otherName = hit.getString("otherName");
                        String totalEpisodes = hit.getString("totalEpisodes");
                mExampleList.add(new ExampleItem(title, img, synopsis, genre, release, status, otherName, totalEpisodes
                                 , ids));
                loadingPop.dismiss();
                Toast.makeText(getActivity(), "Data Refreshed", Toast.LENGTH_SHORT).show();
            }
            mExampleAdapter = new ExampleAdapter(getActivity(), mExampleList);
            mRecyclerView.setAdapter(mExampleAdapter);
            Toast.makeText(getActivity(), "Added Data", Toast.LENGTH_SHORT).show();
            //loadingPop.dismiss();
            }
            catch (JSONException e)
            {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Catch" + e, Toast.LENGTH_LONG).show();
            loadingPop.dismiss();

            }}
        }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            }});
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Ryan M Apr 23 '20 at 02:21
  • http://www.jsonschema2pojo.org/ use this link to create model class for your json. Paste json, select target language as java, source type as json, Annotation style as Gson and click on "Preview". It will create all the necessary classes for you – Mohd Asif Ahmed Apr 23 '20 at 02:24
  • I am using getter model! And json volley at MainActivity so i want to parse those data at MainActivity without Pojo method – RTH Creating Studio Apr 23 '20 at 02:46
  • show your code and what you tried. – Hasan Bou Taam Apr 23 '20 at 11:23
  • I am edited the question and added the MainActivity where the json is calling. Please check again and solve this – RTH Creating Studio Apr 23 '20 at 14:10

1 Answers1

0

I assume the problem is with getting the full episodes data

I will try to rewrite your for loop:

.....
.....

try
{


JSONArray animeArray = response.getJSONArray("anime");

//loop through anime array
for (int i = 0; i < animeArray.length(); i++){

//this should run for every object in anime
JSONObject animeObject = animeArray.getJSONObject(i);

//title
String title = animeObject.getString("title");

//img
String img = animeObject.getString("img");

//synopsis
String synopsis = animeObject.getString("synopsis");

//genres
JSONArray genresArray = animeObject.getJSONArray("genres");
String[] genres = new String[genresArray.length()];
for(int i = 0; i < genresArray.length(); i++){
genres[i] = genresArray.getString(i);
}

//released
int released = animeObject.getInt("released");

//status
String status = animeObject.getString("status");

//otherName
String otherName = animeObject.getString("otherName");

//totalEpisodes
int totalEpisodes = animeObject.getInt("totalEpisodes");


//episodes ids
JSONArray episodesArray = animeObject.getJSONArray("episodes");
String[] ids = new String[episodesArray.length()];
for(int i = 0; i < episodesArray.length(); i++){
ids[i] = episodesArray.getJSONObject(i).getString("id");
}





}//end loop


 mExampleList.add(new ExampleItem(title, img, synopsis, genres, released, status, 
 otherName, totalEpisodes,ids));



........
.........
........

Now you change your ExampleItem constructor

public ExampleItem(String title,String img,String synopsis,String[] genres,int released,String status,String otherName,int totalEpisodes,String[] ids){

  ..................
  .................

 }
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22