xxxxxxxxxx
import 'dart:convert';
void main() {
String dartString = '{"name": "John", "age": 30}';
// Convert Dart string to JSON
var json = jsonDecode(dartString);
print(json['name']); // Output: John
print(json['age']); // Output: 30
}
xxxxxxxxxx
import 'dart:convert';
void main() {
// The string to be converted to JSON
String jsonString = '{"name": "John", "age": 30, "city": "New York"}';
// Convert the string to JSON
Map<String, dynamic> json = jsonDecode(jsonString);
// Access JSON properties
String name = json['name'];
int age = json['age'];
String city = json['city'];
// Print the extracted values
print('Name: $name');
print('Age: $age');
print('City: $city');
}
xxxxxxxxxx
flutter convert json string to json
----------------------
var x1 = '[{"id": "1"}]';
List<dynamic> x2 = jsonDecode(x1);
print(x2[0]);
xxxxxxxxxx
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);
xxxxxxxxxx
import 'dart:convert';
main() {
String objText = '{"name": "bezkoder", "age": 30}';
User user = User.fromJson(jsonDecode(objText));
print(user);
xxxxxxxxxx
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;
}
}
xxxxxxxxxx
class Character {
int? id;
String? name;
String? status;
String? species;
String? type;
String? gender;
Origin? origin;
Origin? location;
String? image;
List<String>? episode;
String? url;
String? created;
Character(
{this.id,
this.name,
this.status,
this.species,
this.type,
this.gender,
this.origin,
this.location,
this.image,
this.episode,
this.url,
this.created});
Character.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
status = json['status'];
species = json['species'];
type = json['type'];
gender = json['gender'];
origin =
json['origin'] != null ? new Origin.fromJson(json['origin']) : null;
location =
json['location'] != null ? new Origin.fromJson(json['location']) : null;
image = json['image'];
episode = json['episode'].cast<String>();
url = json['url'];
created = json['created'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['status'] = this.status;
data['species'] = this.species;
data['type'] = this.type;
data['gender'] = this.gender;
if (this.origin != null) {
data['origin'] = this.origin!.toJson();
}
if (this.location != null) {
data['location'] = this.location!.toJson();
}
data['image'] = this.image;
data['episode'] = this.episode;
data['url'] = this.url;
data['created'] = this.created;
return data;
}
}
class Origin {
String? name;
String? url;
Origin({this.name, this.url});
Origin.fromJson(Map<String, dynamic> json) {
name = json['name'];
url = json['url'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['url'] = this.url;
return data;
}
}
xxxxxxxxxx
class BannersModel {
bool? status;
List<Banners>? banners;
BannersModel({this.status, this.banners});
BannersModel.fromJson(Map<String, dynamic> json) {
status = json['status'];
if (json['banners'] != null) {
banners = <Banners>[];
json['banners'].forEach((v) {
banners!.add(new Banners.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
if (this.banners != null) {
data['banners'] = this.banners!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Banners {
String? id;
String? image;
Banners({this.id, this.image});
Banners.fromJson(Map<String, dynamic> json) {
id = json['id'];
image = json['image'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['image'] = this.image;
return data;
}
}
xxxxxxxxxx
class franchiserequest {
String? responseCode;
String? responseMessage;
List<GetRequests>? getRequests;
franchiserequest({this.responseCode, this.responseMessage, this.getRequests});
franchiserequest.fromJson(Map<String, dynamic> json) {
responseCode = json['response_code'];
responseMessage = json['response_message'];
if (json['get_Requests'] != null) {
getRequests = <GetRequests>[];
json['get_Requests'].forEach((v) {
getRequests!.add(new GetRequests.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['response_code'] = this.responseCode;
data['response_message'] = this.responseMessage;
if (this.getRequests != null) {
data['get_Requests'] = this.getRequests!.map((v) => v.toJson()).toList();
}
return data;
}
}
class GetRequests {
String? tRANSACTIONID;
String? lOCATIONCODE;
String? aCTION;
String? rEQUESTTYPE;
String? aPIMETHOD;
BODY? bODY;
String? rESPONSE;
String? rEQUESTSTATUS;
GetRequests(
{this.tRANSACTIONID,
this.lOCATIONCODE,
this.aCTION,
this.rEQUESTTYPE,
this.aPIMETHOD,
this.bODY,
this.rESPONSE,
this.rEQUESTSTATUS});
GetRequests.fromJson(Map<String, dynamic> json) {
tRANSACTIONID = json['TRANSACTION_ID'];
lOCATIONCODE = json['LOCATION_CODE'];
aCTION = json['ACTION'];
rEQUESTTYPE = json['REQUEST_TYPE'];
aPIMETHOD = json['API_METHOD'];
bODY = json['BODY'] != null ? new BODY.fromJson(json['BODY']) : null;
rESPONSE = json['RESPONSE'];
rEQUESTSTATUS = json['REQUEST_STATUS'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['TRANSACTION_ID'] = this.tRANSACTIONID;
data['LOCATION_CODE'] = this.lOCATIONCODE;
data['ACTION'] = this.aCTION;
data['REQUEST_TYPE'] = this.rEQUESTTYPE;
data['API_METHOD'] = this.aPIMETHOD;
if (this.bODY != null) {
data['BODY'] = this.bODY!.toJson();
}
data['RESPONSE'] = this.rESPONSE;
data['REQUEST_STATUS'] = this.rEQUESTSTATUS;
return data;
}
}
class BODY {
String? franchiseNumber;
String? ordertype;
List<String>? product;
String? cellNumber;
String? request;
BODY(
{this.franchiseNumber,
this.ordertype,
this.product,
this.cellNumber,
this.request});
BODY.fromJson(Map<String, dynamic> json) {
franchiseNumber = json['FranchiseNumber'];
ordertype = json['ordertype'];
product = json['product'].cast<String>();
cellNumber = json['CellNumber'];
request = json['request'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['FranchiseNumber'] = this.franchiseNumber;
data['ordertype'] = this.ordertype;
data['product'] = this.product;
data['CellNumber'] = this.cellNumber;
data['request'] = this.request;
return data;
}
}
xxxxxxxxxx
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;
}
}