Pankaj Maurya
Pankaj Maurya

Reputation: 9

Making Model class from JSON

Here I am trying to make the model class of this JSON

{
  "debit": 1,
  "train": {
    "number": "14207",
    "name": "PADMAVAT EXP",
    "days": [
      {
        "code": "MON",
        "runs": "Y"
      },
      {
        "code": "TUE",
        "runs": "Y"
      },
      {
        "code": "WED",
        "runs": "Y"
      },
      {
        "code": "THU",
        "runs": "Y"
      },
      {
        "code": "FRI",
        "runs": "Y"
      },
      {
        "code": "SAT",
        "runs": "Y"
      },
      {
        "code": "SUN",
        "runs": "Y"
      }
    ],
    "classes": [
      {
        "code": "2A",
        "available": "Y",
        "name": "SECOND AC"
      },
      {
        "code": "3E",
        "available": "N",
        "name": "3rd AC ECONOMY"
      },
      {
        "code": "3A",
        "available": "Y",
        "name": "THIRD AC"
      },
      {
        "code": "2S",
        "available": "N",
        "name": "SECOND SEATING"
      },
      {
        "code": "CC",
        "available": "N",
        "name": "AC CHAIR CAR"
      },
      {
        "code": "SL",
        "available": "Y",
        "name": "SLEEPER CLASS"
      },
      {
        "code": "FC",
        "available": "N",
        "name": "FIRST CLASS"
      },
      {
        "code": "1A",
        "available": "Y",
        "name": "FIRST AC"
      }
    ]
  },
  "response_code": 200
}

And I try this First I create two Integer variable name response_code and debit(because inside "{" and "}" are key and value pairs) Then I create on array List and one List named as train and classes which represents train list and classes array in json. Still Not Working

public class NewTrain {
    @SerializedName("response_code")
    private int response_code;
    @SerializedName("debit")
    private int debit;
    @SerializedName("train")
    private List<TrainClass> train= null;
   @SerializedName("classes")
    private ArrayList<classes> arrayList = null;

    public int getResponse_code() {
        return response_code;
    }

    public void setResponse_code(int response_code) {
        this.response_code = response_code;
    }

    public int getDebit() {
        return debit;
    }

    public void setDebit(int debit) {
        this.debit = debit;
    }

    public List<TrainClass> getTrain() {
        return train;
    }

    public void setTrain(List<TrainClass> train) {
        this.train = train;
    }

    public ArrayList<classes> getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList<classes> arrayList) {
        this.arrayList = arrayList;
    }
}

public class TrainClass
 @SerializedName("number")
    private int number;
    @SerializedName("name")
    private String name;
    @SerializedName("days")
    private List<Day> dayList = null;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Day> getDayList() {
        return dayList;
    }

    public void setDayList(List<Day> dayList) {
        this.dayList = dayList;
    }
}
public class Day {
    @SerializedName("code")
    private String code;
    @SerializedName("runs")
    private String runs;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getRuns() {
        return runs;
    }

    public void setRuns(String runs) {
        this.runs = runs;
    }

}
public class classes {
    @SerializedName("code")
    private String code;

    @SerializedName("available")
    private String available;

    @SerializedName("name")
    private String name;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getAvailable() {
        return available;
    }

    public void setAvailable(String available) {
        this.available = available;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

But instead it is giving Begin Array but was Begin Object at line 1 column 2!!! Calling API data from server method

 public void Show(View view) {

        MyWebService myWebService = MyWebService.retrofitfortrain.create(MyWebService.class);
        Call<List<NewTrain>>  call = myWebService.getTrainList();
      call.enqueue(new Callback<List<NewTrain>>() {
          @Override
          public void onResponse(Call<List<NewTrain>> call, Response<List<NewTrain>> response) {
              if(response.isSuccessful())
              {
                  for(NewTrain newTrain: response.body()) {
                      t.append(String.valueOf(newTrain.getResponse_code()));
                  }
              }
          }

          @Override
          public void onFailure(Call<List<NewTrain>> call, Throwable t) {
              Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();

          }
      });

    }

Upvotes: 0

Views: 62

Answers (3)

Faysal Ahmed
Faysal Ahmed

Reputation: 7669

As the JSON mentioned train contains a JSON object, not a list. Another of the classes list will be a member of TainClass not in the NewTrainClass.

So, instead of:

@SerializedName("train")
private List<TrainClass> train= null;

make it

@SerializedName("train")
private TrainClass train= null;

and also Move Classes to the TrainClass

@SerializedName("classes")
private ArrayList<classes> arrayList = null; 

Finally, the NewTrain Object will be like

public class NewTrain {
    @SerializedName("response_code")
    private int response_code;
    @SerializedName("debit")
    private int debit;
    @SerializedName("train")
    private TrainClass train;

    public int getResponse_code() {
        return response_code;
    }

    public void setResponse_code(int response_code) {
        this.response_code = response_code;
    }

    public int getDebit() {
        return debit;
    }

    public void setDebit(int debit) {
        this.debit = debit;
    }

    public TrainClass getTrain() {
        return train;
    }

    public void setTrain(TrainClass train) {
        this.train = train;
    }
}

public class TrainClass
 @SerializedName("number")
    private int number;
    @SerializedName("name")
    private String name;
    @SerializedName("days")
    private List<Day> dayList = null;
   @SerializedName("classes")
    private ArrayList<classes> arrayList = null;
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Day> getDayList() {
        return dayList;
    }

    public void setDayList(List<Day> dayList) {
        this.dayList = dayList;
    }

    public ArrayList<classes> getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList<classes> arrayList) {
        this.arrayList = arrayList;
    }
}

Look like the JSON contains only a NewTrain, not the List<NewTrain>. nd also need to update the myWebService.getTrainList(); You also need to update the method like

 public void Show(View view) {

    MyWebService myWebService = MyWebService.retrofitfortrain.create(MyWebService.class);
    Call<NewTrain>  call = myWebService.getTrainList();
  call.enqueue(new Callback<NewTrain>() {
      @Override
      public void onResponse(Call<NewTrain> call, Response<NewTrain> response) {
          if(response.isSuccessful())
          {
              for(NewTrain newTrain: response.body()) {
                  t.append(String.valueOf(newTrain.getResponse_code()));
              }
          }
      }

      @Override
      public void onFailure(Call<NewTrain> call, Throwable t) {
          Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();

      }
  });

}

Hope this will work.

Upvotes: 0

Trilok Singh
Trilok Singh

Reputation: 1363

try this

 export class RootObject {
    debit: number;
    train: Train;
    response_code: number;
}

export interface Train {
    number: string;
    name: string;
    days: Day[];
    classes: ClassModel[];
}
export interface Day {
    code: string;
    runs: string;
}

export interface ClassModel {
    code: string;
    available: string;
    name: string;
}

Upvotes: 1

Sikakollu
Sikakollu

Reputation: 56

According to the json mentioned train is not a list, but in the class you made it a list. May be that is the issue. So , instead of:

@SerializedName("train")
private List<TrainClass> train= null;

make it

@SerializedName("train")
private TrainClass train= null;

and update the getter, setter methods accordingly

Upvotes: 0

Related Questions