Answers for "parse json to dart model"

1

parse json to dart model

import 'dart:convert';

main() {
  String nestedObjText =
      '{"title": "Dart Tutorial", "description": "Way to parse Json", "author": {"name": "bezkoder", "age": 30}}';

  Tutorial tutorial = Tutorial.fromJson(jsonDecode(nestedObjText));

  print(tutorial);
Posted by: Guest on January-22-2021
4

flutter generate json files

flutter packages pub run build_runner build
Posted by: Guest on July-17-2020
0

dart to json

class Testing {
  String guid;
  int index;
  String favoriteFruit;
  double latitude;
  String company;
  String email;
  String picture;
  List<String> tags;
  String registered;
  String eyeColor;
  String phone;
  String address;
  List<Friends> friends;
  bool isActive;
  String about;
  String balance;
  String name;
  String gender;
  int age;
  String greeting;
  double longitude;
  String sId;

  Testing(
      {this.guid,
      this.index,
      this.favoriteFruit,
      this.latitude,
      this.company,
      this.email,
      this.picture,
      this.tags,
      this.registered,
      this.eyeColor,
      this.phone,
      this.address,
      this.friends,
      this.isActive,
      this.about,
      this.balance,
      this.name,
      this.gender,
      this.age,
      this.greeting,
      this.longitude,
      this.sId});

  Testing.fromJson(Map<String, dynamic> json) {
    guid = json['guid'];
    index = json['index'];
    favoriteFruit = json['favoriteFruit'];
    latitude = json['latitude'];
    company = json['company'];
    email = json['email'];
    picture = json['picture'];
    tags = json['tags'].cast<String>();
    registered = json['registered'];
    eyeColor = json['eyeColor'];
    phone = json['phone'];
    address = json['address'];
    if (json['friends'] != null) {
      friends = new List<Friends>();
      json['friends'].forEach((v) {
        friends.add(new Friends.fromJson(v));
      });
    }
    isActive = json['isActive'];
    about = json['about'];
    balance = json['balance'];
    name = json['name'];
    gender = json['gender'];
    age = json['age'];
    greeting = json['greeting'];
    longitude = json['longitude'];
    sId = json['_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['guid'] = this.guid;
    data['index'] = this.index;
    data['favoriteFruit'] = this.favoriteFruit;
    data['latitude'] = this.latitude;
    data['company'] = this.company;
    data['email'] = this.email;
    data['picture'] = this.picture;
    data['tags'] = this.tags;
    data['registered'] = this.registered;
    data['eyeColor'] = this.eyeColor;
    data['phone'] = this.phone;
    data['address'] = this.address;
    if (this.friends != null) {
      data['friends'] = this.friends.map((v) => v.toJson()).toList();
    }
    data['isActive'] = this.isActive;
    data['about'] = this.about;
    data['balance'] = this.balance;
    data['name'] = this.name;
    data['gender'] = this.gender;
    data['age'] = this.age;
    data['greeting'] = this.greeting;
    data['longitude'] = this.longitude;
    data['_id'] = this.sId;
    return data;
  }
}

class Friends {
  int id;
  String name;

  Friends({this.id, this.name});

  Friends.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  }
}
Posted by: Guest on July-23-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language