Making Model class from JSON

Related searches

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();

          }
      });

    }

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;
}

How to create model class for json parsing?, Generate Plain Old Java Objects from JSON or JSON-Schema. Make classes serializable. Make classes parcelable public class MyObject { private java.util. Generate C# Class from JSON. Use this tool to quickly generate model classes for C# from a sample JSON document. The csharp model class is annotated using JsonProperty attribute supplied by Newtonsoft. JSON. JavaScript Object Notation (JSON) pronounced as "Jason" is the de facto standard for data interchange on the web these days. It is a


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

jsonschema2pojo, This guide describes the process of converting JSON data retrieved from a Let's create a Java class that will act as the Business model in our application: Click the convert button and wait a few seconds until your C# classes appear. Step 3 : Copy the retuned C# classes from the second editor and deserialize using the 'Root' class. When you copy the returned classes in the directory of your solution, you can deserialize your JSON response using the 'Root' class using any deserializer like Newtonsoft.


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.

Converting JSON to Models, Generate models and serializers from JSON, schema, and GraphQL for working with data class MyData { people: Person[]; static fromJson(json: string) {…}� JSON Utils is a site for generating C#, VB.Net, Javascript, Java and PHP classes from JSON. It will also clean up your JSON and show a data viewer to assist you while you are developing


Convert JSON to Swift, C#, TypeScript, Objective-C, Go, Java, C++ , Analyzing sample data cross. Generate C# classes with Json.NET attributes from JSON, JSON Schema, and GraphQL queries. Generate C# NowInstall Visual� Creating model classes the json_serializable way. The following shows how to convert the User class to a json_serializable class. For the sake of simplicity, this code uses the simplified JSON model from the previous samples. user.dart


Instantly generate C# models and helper methods from JSON., Free code generator which converts your JSON (JavaScript Object Notation) schema into Java Object. The JSON keys are converted to private variables with getter setter methods for them. The inner objects in JSON are converted as inner classes in Java Object. Copy the converted JAVA code and make it work for you.


How to serialize and deserialize (marshal and unmarshal) JSON in .NET. 05/13/2020; 28 minutes to read +1; In this article. This article shows how to use the System.Text.Json namespace to serialize and deserialize to and from JavaScript Object Notation (JSON).