PrincE
PrincE

Reputation: 41

How to parse json Data Android

How to parse the series with same title to one array list

so i got Title name with season 1 and 2

What is the best way to do it

My Json data

{
"series":[
{
"title":"Jumping cat", "genre":"comedy", "year":2018, "season":1, "imdb":7, "info":"comdey series", "episodes":10, "cover":"poster" }, {
"title":"Jumping cat", "genre":"comedy", "year":2019, "season":2, "imdb":7, "info":"comdey series", "episodes":11, "cover":"poster" } ] }

Upvotes: 0

Views: 93

Answers (5)

Riskhan
Riskhan

Reputation: 4470

You can use Google's gson library for simply parse json into java classe and vice versa. An example for how to use gson found here

Upvotes: 0

elcuco
elcuco

Reputation: 9208

If you don't want to add another 3rd party. You can do this in few lines. Yes, its manual labor, but it will save a lot of bytecode added to your APK.

    public class Serie {
        public String title;
        public String genere;
        public int year;
        public int season;
        public int imdb;
        public String info;
        public int episodes;
        public String cover;

        public static Serie toObject(JSONObject o) throws JSONException {
            Serie s = new Serie();
            s.title = o.getString("title");
            s.genere = o.getString("genre");
            s.year = o.getInt("year");
            s.season = o.getInt("season");
            s.imdb = o.getInt("imdb");
            s.info = o.getString("info");
            s.episodes = o.getInt("episodes");
            s.cover = o.getString("cover");
            return s;
        }

         public static List<Serie> toArray(String json) throws JSONException {
            JSONObject oo = new JSONObject(json);
            JSONArray a = oo.getJSONArray("series");
            List<Serie> l = new ArrayList<>(a.length());
            for (int i =0; i<a.length(); i++ ) {
                JSONObject o = a.getJSONObject(i);
                l.add(Serie.toObject(o));
            }
            return l;
        }
    }

    // usage 
    try {
       List<Serie> ll = Serie.toArray(s);
       System.out.println(ll.toString());
    } catch (JSONException e) {
       e.printStackTrace();
    }

Upvotes: 0

MustafaShaikh
MustafaShaikh

Reputation: 152

Your response starts with list

@SerializedName("series")
@Expose
private List<Series> series = null;

List model class

@SerializedName("title")
@Expose
private String title;
@SerializedName("genre")
@Expose
private String genre;
@SerializedName("year")
@Expose
private Integer year;
@SerializedName("season")
@Expose
private Integer season;
@SerializedName("imdb")
@Expose
private Integer imdb;
@SerializedName("info")
@Expose
private String info;
@SerializedName("episodes")
@Expose
private Integer episodes;
@SerializedName("cover")
@Expose
private String cover;

And create getter setter method

Upvotes: 0

HeinerTheBest
HeinerTheBest

Reputation: 216

For get the information of the Json you always will need two things

  1. POJO's the model class ob the object you will get
  2. Choose wich one to use or JsonParser who is native of Java or Gson who is a third party

I hope this can help you :D

Upvotes: 0

Amir MB
Amir MB

Reputation: 3418

The following code will create a "HashMap" with String keys and ArrayList values. The ArrayList include your model for each series:

try{
    JSONObject reader = new JSONObject(str);
    JSONArray array = reader.optJSONArray("series");
    HashMap<String, ArrayList<YourModel>> map =  new HashMap<>();
    for(int i=0;i<array.length();i++){
        JSONObject innerObject = array.getJSONObject(i);
        if(map.get(innerObject.getString("title")) != null){ // check if the title already exists, then add it to it's list
            ArrayList<YourModel> arrayList = map.get(innerObject.getString("title"));
            arrayList.add(new YourModel(innerObject));
        }else{ // if the title does not exist, create new ArrayList
            ArrayList<YourModel> arrayList = new ArrayList<>();
            arrayList.add(new YourModel(innerObject));
            map.put(innerObject.getString("title"),arrayList);
        }
    }
}catch (JSONException e){
    // Do error handling
}

Upvotes: 1

Related Questions