“Json to Dart” Ответ

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);
Mysterious Mongoose

преобразовать JSON в Dart

class TrendingMoviesModel {
  String? name;
  String? backdropPath;
  List<int>? genreIds;
  String? originalLanguage;
  String? posterPath;
  List<String>? originCountry;
  String? overview;
  String? mediaType;

  TrendingMoviesModel(
      {this.name,
      this.backdropPath,
      this.genreIds,
      this.originalLanguage,
      this.posterPath,
      this.originCountry,
      this.overview,
      this.mediaType});

  TrendingMoviesModel.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    backdropPath = json['backdrop_path'];
    genreIds = json['genre_ids'].cast<int>();
    originalLanguage = json['original_language'];
    posterPath = json['poster_path'];
    originCountry = json['origin_country'].cast<String>();
    overview = json['overview'];
    mediaType = json['media_type'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['backdrop_path'] = this.backdropPath;
    data['genre_ids'] = this.genreIds;
    data['original_language'] = this.originalLanguage;
    data['poster_path'] = this.posterPath;
    data['origin_country'] = this.originCountry;
    data['overview'] = this.overview;
    data['media_type'] = this.mediaType;
    return data;
  }
}
Itchy Iguana

Json to Dart

class CoursesModel {
  List<Data>? data;

  CoursesModel({this.data});

  CoursesModel.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(new Data.fromJson(v));
      });
    }
  }

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

class Data {
  int? id;
  String? status;
  String? createdOn;
  String? title;
  Thumbnail? thumbnail;
  String? summery;
  String? slug;
  Category? category;
  String? link;
  String? coupon;

  Data(
      {this.id,
      this.status,
      this.createdOn,
      this.title,
      this.thumbnail,
      this.summery,
      this.slug,
      this.category,
      this.link,
      this.coupon});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    status = json['status'];
    createdOn = json['created_on'];
    title = json['title'];
    thumbnail = json['thumbnail'] != null
        ? new Thumbnail.fromJson(json['thumbnail'])
        : null;
    summery = json['summery'];
    slug = json['slug'];
    category = json['category'] != null
        ? new Category.fromJson(json['category'])
        : null;
    link = json['link'];
    coupon = json['coupon'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['status'] = this.status;
    data['created_on'] = this.createdOn;
    data['title'] = this.title;
    if (this.thumbnail != null) {
      data['thumbnail'] = this.thumbnail!.toJson();
    }
    data['summery'] = this.summery;
    data['slug'] = this.slug;
    if (this.category != null) {
      data['category'] = this.category!.toJson();
    }
    data['link'] = this.link;
    data['coupon'] = this.coupon;
    return data;
  }
}

class Thumbnail {
  Data? data;

  Thumbnail({this.data});

  Thumbnail.fromJson(Map<String, dynamic> json) {
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

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

class Data {
  String? fullUrl;
  String? url;
  String? assetUrl;
  List<Thumbnails>? thumbnails;
  Null? embed;

  Data({this.fullUrl, this.url, this.assetUrl, this.thumbnails, this.embed});

  Data.fromJson(Map<String, dynamic> json) {
    fullUrl = json['full_url'];
    url = json['url'];
    assetUrl = json['asset_url'];
    if (json['thumbnails'] != null) {
      thumbnails = <Thumbnails>[];
      json['thumbnails'].forEach((v) {
        thumbnails!.add(new Thumbnails.fromJson(v));
      });
    }
    embed = json['embed'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['full_url'] = this.fullUrl;
    data['url'] = this.url;
    data['asset_url'] = this.assetUrl;
    if (this.thumbnails != null) {
      data['thumbnails'] = this.thumbnails!.map((v) => v.toJson()).toList();
    }
    data['embed'] = this.embed;
    return data;
  }
}

class Thumbnails {
  String? key;
  String? url;
  String? relativeUrl;
  String? dimension;
  int? width;
  int? height;

  Thumbnails(
      {this.key,
      this.url,
      this.relativeUrl,
      this.dimension,
      this.width,
      this.height});

  Thumbnails.fromJson(Map<String, dynamic> json) {
    key = json['key'];
    url = json['url'];
    relativeUrl = json['relative_url'];
    dimension = json['dimension'];
    width = json['width'];
    height = json['height'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['key'] = this.key;
    data['url'] = this.url;
    data['relative_url'] = this.relativeUrl;
    data['dimension'] = this.dimension;
    data['width'] = this.width;
    data['height'] = this.height;
    return data;
  }
}

class Category {
  int? id;

  Category({this.id});

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

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

Json to Dart

class Autogenerated {
  String foodOrderOption;
  String foodOrder;

  Autogenerated({this.foodOrderOption, this.foodOrder});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    foodOrderOption = json['foodOrderOption'];
    foodOrder = json['foodOrder'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['foodOrderOption'] = this.foodOrderOption;
    data['foodOrder'] = this.foodOrder;
    return data;
  }
}
Helpless Hoopoe

преобразовать JSON в Dart

class NewsDescriptionModel {
  NewsDetail? newsDetail;
  String? editorList;
  List<Tags>? tags;

  NewsDescriptionModel({this.newsDetail, this.editorList, this.tags});

  NewsDescriptionModel.fromJson(Map<String, dynamic> json) {
    newsDetail = json['NewsDetail'] != null
        ? new NewsDetail.fromJson(json['NewsDetail'])
        : null;
    editorList = json['editorList'];
    if (json['tags'] != null) {
      tags = <Tags>[];
      json['tags'].forEach((v) {
        tags!.add(new Tags.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.newsDetail != null) {
      data['NewsDetail'] = this.newsDetail!.toJson();
    }
    data['editorList'] = this.editorList;
    if (this.tags != null) {
      data['tags'] = this.tags!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class NewsDetail {
  String? id;
  String? source;
  String? author;
  String? title;
  String? timestamp;
  String? section;
  String? slug;
  String? sectionId;
  String? content;
  String? websiteurl;
  String? thumbnailUrl;
  String? sectionUrl;
  String? url;
  String? newsType;
  String? highlights;
  String? comments;

  NewsDetail(
      {this.id,
      this.source,
      this.author,
      this.title,
      this.timestamp,
      this.section,
      this.slug,
      this.sectionId,
      this.content,
      this.websiteurl,
      this.thumbnailUrl,
      this.sectionUrl,
      this.url,
      this.newsType,
      this.highlights,
      this.comments});

  NewsDetail.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    source = json['source'];
    author = json['author'];
    title = json['title'];
    timestamp = json['timestamp'];
    section = json['section'];
    slug = json['slug'];
    sectionId = json['section_id'];
    content = json['content'];
    websiteurl = json['websiteurl'];
    thumbnailUrl = json['thumbnail_url'];
    sectionUrl = json['section_url'];
    url = json['url'];
    newsType = json['news_type'];
    highlights = json['highlights'];
    comments = json['comments'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['source'] = this.source;
    data['author'] = this.author;
    data['title'] = this.title;
    data['timestamp'] = this.timestamp;
    data['section'] = this.section;
    data['slug'] = this.slug;
    data['section_id'] = this.sectionId;
    data['content'] = this.content;
    data['websiteurl'] = this.websiteurl;
    data['thumbnail_url'] = this.thumbnailUrl;
    data['section_url'] = this.sectionUrl;
    data['url'] = this.url;
    data['news_type'] = this.newsType;
    data['highlights'] = this.highlights;
    data['comments'] = this.comments;
    return data;
  }
}

class Tags {
  String? title;
  int? topicID;
  String? sectionPageURL;

  Tags({this.title, this.topicID, this.sectionPageURL});

  Tags.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    topicID = json['topicID'];
    sectionPageURL = json['sectionPageURL'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['topicID'] = this.topicID;
    data['sectionPageURL'] = this.sectionPageURL;
    return data;
  }
}
Priyanka Bhosale

Ответы похожие на “Json to Dart”

Вопросы похожие на “Json to Dart”

Больше похожих ответов на “Json to Dart” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования