Mapping JSON into Class Objects

后端 未结 5 902
你的背包
你的背包 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:43

    I created some useful library for this using reflection called json_parser which is available at pub.

    https://github.com/gi097/json_parser

    You can add the following to your dependencies.yaml:

    dependencies:
      json_parser: 0.1.1
      build_runner: 0.8.3
    

    Then the json can be parsed using:

    DataClass instance = JsonParser.parseJson<DataClass>(json);
    

    Follow the README.md for more instructions.

    0 讨论(0)
  • 2020-12-19 05:50

    The best solution I've found is this medium post

    Which converts the Json to dart very easily

    import 'package:json_annotation/json_annotation.dart';
    
    part 'post_model.g.dart';
    
    @JsonSerializable()
    class PostModel {
      int userId;
      int id;
      String title;
      String body;
      PostModel(this.userId, this.id, this.title, this.body);
      factory PostModel.fromJson(Map<String, dynamic> json) => _$PostModelFromJson(json);
      Map<String, dynamic> toJson() => _$PostModelToJson(this);
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 05:54

    this pkg can help you convert JSON to a class instance. https://www.npmjs.com/package/class-converter

    import { property, toClass } from 'class-convert';
    
    class UserModel {
      @property('i')
      id: number;
    
      @property()
      name: string;
    }
    
    const userRaw = {
      i: 1234,
      name: 'name',
    };
    
    // use toClass to convert plain object to class
    const userModel = toClass(userRaw, UserModel);
    // you will get a class, just like below one
    {
      id: 1234,
      name: 'name',
    }
    
    0 讨论(0)
  • 2020-12-19 06:00

    If you want to get your JSON from a url do as follows:

    import 'dart:convert';
    
    _toObject() async {
      var url = 'YourJSONurl';
      var httpClient  = createHttpClient();
      var response =await httpClient.get(url);
      Map cardInfo = JSON.decode(response.body);
      var ci = new CardInfo.fromJson(cardInfo);
    }
    

    Please refer to the main answer if you want to know how to setup your class so that your JSON fields can be mapped to it. It is very helpful.

    0 讨论(0)
提交回复
热议问题