从JSON生成模型类
在这里,我试图创建这个JSON的模型类从JSON生成模型类,json,Json,在这里,我试图创建这个JSON的模型类 { "debit": 1, "train": { "number": "14207", "name": "PADMAVAT EXP", "days": [ { "code": "MON", "runs": "Y" }, { "code": "TUE", "runs": "Y" }, {
{
"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
}
我试试这个
首先,我创建两个整数变量名response_code和debit(因为在“{”和“}”中是键和值对)
然后我在数组列表上创建一个名为train和classes的列表,它用json表示train列表和classes数组。仍然不起作用
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;
}
}
公共类NewTrain{
@SerializedName(“响应代码”)
专用int响应_码;
@序列化名称(“借方”)
私人借记卡;
@序列化名称(“列车”)
私有列表列=空;
@序列化名称(“类”)
私有ArrayList ArrayList=null;
公共int getResponse_代码(){
返回应答码;
}
公共无效setResponse_代码(int response_代码){
this.response\u code=response\u code;
}
public int getDebit(){
返回借方;
}
公共作废设置借方(整数借方){
this.debit=借方;
}
公共列表getTrain(){
回程列车;
}
公共列车(列表列车){
这个火车=火车;
}
公共ArrayList getArrayList(){
返回数组列表;
}
公共无效setArrayList(ArrayList ArrayList){
this.arrayList=arrayList;
}
}
公共班火车班
@序列化名称(“编号”)
私有整数;
@序列化名称(“名称”)
私有字符串名称;
@序列化名称(“天”)
私有列表dayList=null;
public int getNumber(){
返回号码;
}
公共无效集合号(整数){
这个数字=数字;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共列表getDayList(){
返回日列表;
}
公共void setDayList(列表dayList){
this.dayList=dayList;
}
}
公课日{
@序列化名称(“代码”)
私有字符串码;
@SerializedName(“运行”)
私人字符串运行;
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getRuns(){
回程;
}
公共无效集合运行(字符串运行){
this.runs=运行;
}
}
公共班级{
@序列化名称(“代码”)
私有字符串码;
@SerializedName(“可用”)
私有字符串可用;
@序列化名称(“名称”)
私有字符串名称;
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getAvailable(){
返回可用;
}
public void setAvailable(字符串可用){
this.available=可用;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
但它给出的是Begin数组,但却是第1行第2列的Begin对象!!!
从服务器方法调用API数据
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();
}
});
}
公共作废显示(视图){
MyWebService MyWebService=MyWebService.ReformationForTrain.create(MyWebService.class);
Call Call=myWebService.getTrainList();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful())
{
for(NewTrain NewTrain:response.body()){
t、 append(String.valueOf(newTrain.getResponse_code());
}
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
根据前面提到的json,train不是一个列表,但在课堂上你把它列为一个列表。也许这就是问题所在。因此,不是:
@SerializedName("train")
private List<TrainClass> train= null;
@SerializedName("train")
private List<TrainClass> train= null;
并相应地更新getter、setter方法,因为前面提到的JSON
train
包含一个JSON对象,而不是列表。另一个classes
列表将是TainClass
的成员,而不在NewTrainClass
中
因此,不是:
@SerializedName("train")
private List<TrainClass> train= null;
@SerializedName("train")
private List<TrainClass> train= null;
并将类
移动到列车类
@SerializedName("classes")
private ArrayList<classes> arrayList = null;
希望这能奏效。试试这个
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;
}
可以我要试试,它不管用!!请查看有问题的请求方法!!请求数据时是否有任何错误我也做了同样的操作,但仍然得到异常!!!我还编辑了有问题的请求方法!!看看你说的maketrain类对象,但是在你的新代码中你也使用了List。同样的错误!!应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$Yes!!但在foreach循环中,它显示的每个循环在这里都不适用
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();
}
});
}
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;
}