flutter json 처리

  • json data 처리 하는 클래스를  여러군데 선언 하고  import하는 경우 다른 이름으로 사용 하여야 한다.
  • 기존의 return type을 사용 하는 경우도 생각을 해 볼 수 있다. 

{
    "easyInfo": {
        "diff_amt": -1233280,
        "with_cust": 0,
        "tr_dt": "20201213",
        "exp_amt": 1233280,
        "amt": 1633000,
        "exp_dt": "20201231",
        "exp_sum": 21526436,
        "hold_amt": 0
    }
}

클래스안에서 변수명이 일치 하면 error  발생


class Maps {
EasyInfo easyInfo;
BizfundInfo bizfundInfo;

Maps({this.easyInfo, this.bizfundInfo});

Maps.fromJson(Map<String, dynamic> json) {
easyInfo = json['easyInfo'] != null
? new EasyInfo.fromJson(json['easyInfo'])
: null;
bizfundInfo = json['bizfundInfo'] != null
? new BizfundInfo.fromJson(json['bizfundInfo'])
: null;
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.easyInfo != null) {
data['easyInfo'] = this.easyInfo.toJson();
}
if (this.bizfundInfo != null) {
data['bizfundInfo'] = this.bizfundInfo.toJson();
}
return data;
}
}

class EasyInfo {
int diffAmt;
int withCust;
String trDt;
int expAmt;
int amt;
String expDt;
int expSum;
int holdAmt;

EasyInfo(
{this.diffAmt,
this.withCust,
this.trDt,
this.expAmt,
this.amt,
this.expDt,
this.expSum,
this.holdAmt});

EasyInfo.fromJson(Map<String, dynamic> json) {
diffAmt = json['diff_amt'];
withCust = json['with_cust'];
trDt = json['tr_dt'];
expAmt = json['exp_amt'];
amt = json['amt'];
expDt = json['exp_dt'];
expSum = json['exp_sum'];
holdAmt = json['hold_amt'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['diff_amt'] = this.diffAmt;
data['with_cust'] = this.withCust;
data['tr_dt'] = this.trDt;
data['exp_amt'] = this.expAmt;
data['amt'] = this.amt;
data['exp_dt'] = this.expDt;
data['exp_sum'] = this.expSum;
data['hold_amt'] = this.holdAmt;
return data;
}
}

class BizfundInfo {
int withCusts;

BizfundInfo({this.withCusts});

BizfundInfo.fromJson(Map<String, dynamic> json) {
withCusts = json['with_cust'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['with_cust'] = this.withCusts;
return data;
}
}

댓글

이 블로그의 인기 게시물

자바 string 비교 할때 equlas 를 사용하자

jqgrid 에서 export execl 붙이기

flutter webview scroll 안되는 경우 gestureRecognizers: gestureRecognizers, 추가 해서 되었다