Mapping JSON into Class Objects

后端 未结 5 906
你的背包
你的背包 2020-12-19 05:15

I am trying to map my JSON file into a class object, and then update the cards based on the newly received JSON.

My JSON structure is like this

 {
          


        
5条回答
  •  甜味超标
    2020-12-19 05:51

    class CardInfo {
      //Constructor
      String id;
      String description;
      String role;
      int score;
    
      CardInfo.fromJson(Map json) {
        this.id = json['id'];
        this.description = json['description'];
        this.role = json['Role'];
        this.score = json['score'];
      }
    }
    
    var ci = new CardInfo.fromJson(myJson); 
    

    You can use source generation tools like https://github.com/dart-lang/source_gen https://pub.dartlang.org/packages/json_serializable to generate the serialization and deserialization code for you.

    If you prefer using immutable classes https://pub.dartlang.org/packages/built_value is a good bet.

提交回复
热议问题