Flutter

Flutter - ToJson

kakaroo 2022. 2. 6. 13:42
반응형

article logo

 

<ToJson>

01_id: 0, 02_cityName: 서울

[{build_cost: 22, sell_cost: 11, buildingName: , buy_cost: 44, toll_cost: 8},

{sell_cost: 4, build_cost: 8, buildingName: 빌라, buy_cost: 16, toll_cost: 16},

{build_cost: 24, sell_cost: 12, buildingName: 빌딩, buy_cost: 48, toll_cost: 34},

{sell_cost: 20, build_cost: 40, buildingName: 호텔, buy_cost: 80, toll_cost: 70},

{build_cost: 40, sell_cost: 20, buildingName: 랜드마크, buy_cost: 0, toll_cost: 60}]

 

class Todo {

           final int userId;

           final int id;

           final String title;

           Todo(this.userId, this.id, this.title);

           factory Todo.fromJson(

                      Map<String, dynamic> json,

                      ) {

                      return Todo(

                                 json['userID'] as int,

                                 json['id'] as int,

                                 json['title'] as String

                      );

           }

          

           Map<String, dynamic> toJson(Todo instance) =>

                      <String, dynamic> {

                                 'userId': instance.userID,

                                 'id': instance.id,

                                 'title': instance.title

                      };

                     

}

 

위 코드는 json_serializable 패키지(JSON 직렬화)를 이용해 자동 생성이 가능하다.

반응형