json to class dart
//insideclass
ClassName.fromJson(Map<String, dynamic> json) {
variable1 = json['variable11'];
variable2 = json['variable12'];
variable3 = json['variable13']['variable14'];
}
//uses
//after http request or whatever
Map<dynamic, dynamic> res = await jsonDecode(response.body.toString());
Classname.fromJson(res);
3.63
8
The Data:
For this example, let's assume that my data looks something like this:
{
"type": "articles",
"id": "1",
"data": [
"val",
"val2"
],
"author": {
"id": "42",
"name": "bo"
}
}
This data object is rather simple but will help us highlight all the important cases that we care about.
Our Classes:
In this example, we have 2 classes that we are working with: Document.dart
class Document {
final String type;
final int id;
final List<String> data;
final Author author;
Document({this.type, this.id, this.data, this.author});
// add fromJson method here
}
and our Author.dart
class Author{
final int id;
final String name;
Author({this.id, this.name});
// add fromJson method here
}
FromJson Methods:
Let’s first start with our Author class as it has no nested class objects in it.
class Author{
final int id;
final String name;
Author({this.id, this.name});
factory Author.fromJson(Map<String, dynamic> json) {
return Author(
id: json["id"],
name: json["name"],
);
}
}
As you can see, it’s a rather simple method when you are dealing with just converting simple variables from JSON into your class objects. To ensure we don’t have any issues with the format in which the data is received from our APIs, you can go a step further and cast the JSON values into your object type like so:
json["name"].toString()
This is not necessary, but sometimes useful if the source of the API is not reliable or a professional service. I’ve seen instances in the past where when a double is equal to 0, the JSON object reads it as an int instead of a double which then throws an error. Casting your value into a variable type is a safe way of handling those rare cases.
Next, let’s take a look at our parent class, Document:
class Document {
final String type;
final int id;
final List<String> data;
final Author author;
Document({this.type, this.id, this.data, this.author});
factory Document.fromJson(Map<String, dynamic> json) {
var dataObj = json['data'];
return Document(
type: json["type"],
id: json["id"],
data: new List<String>.from(dataObj),
author: Author.fromJson(json['author']),
);
}
}
Thank you!
8
0
3
1
class Autogenerated {
int userId;
int id;
String title;
String body;
Autogenerated({this.userId, this.id, this.title, this.body});
Autogenerated.fromJson(Map<String, dynamic> json) {
userId = json['userId'];
id = json['id'];
title = json['title'];
body = json['body'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['userId'] = this.userId;
data['id'] = this.id;
data['title'] = this.title;
data['body'] = this.body;
return data;
}
}
Thank you!
1
0
0
0
class BusinessProfileEditModel {
String name;
String address;
String email;
String phoneNumber;
String rcNumber;
String businessRegistrationTypeKey;
String businessVerticalKey;
String countryKey;
String lgaKey;
BusinessProfileEditModel(
{this.name,
this.address,
this.email,
this.phoneNumber,
this.rcNumber,
this.businessRegistrationTypeKey,
this.businessVerticalKey,
this.countryKey,
this.lgaKey});
BusinessProfileEditModel.fromJson(Map<String, dynamic> json) {
name = json['name'];
address = json['address'];
email = json['email'];
phoneNumber = json['phoneNumber'];
rcNumber = json['rcNumber'];
businessRegistrationTypeKey = json['businessRegistrationTypeKey'];
businessVerticalKey = json['businessVerticalKey'];
countryKey = json['countryKey'];
lgaKey = json['lgaKey'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['address'] = this.address;
data['email'] = this.email;
data['phoneNumber'] = this.phoneNumber;
data['rcNumber'] = this.rcNumber;
data['businessRegistrationTypeKey'] = this.businessRegistrationTypeKey;
data['businessVerticalKey'] = this.businessVerticalKey;
data['countryKey'] = this.countryKey;
data['lgaKey'] = this.lgaKey;
return data;
}
}
Thank you!
0
0
0
1
class Todo {
int userId;
int id;
String title;
bool completed;
Todo({this.userId, this.id, this.title, this.completed});
Todo.fromJson(Map<String, dynamic> json) {
userId = json['userId'];
id = json['id'];
title = json['title'];
completed = json['completed'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['userId'] = this.userId;
data['id'] = this.id;
data['title'] = this.title;
data['completed'] = this.completed;
return data;
}
}
Thank you!
1
0
4.22
9
class Autogenerated {
int postId;
int id;
String name;
String email;
String body;
Autogenerated({this.postId, this.id, this.name, this.email, this.body});
Autogenerated.fromJson(Map<String, dynamic> json) {
postId = json['postId'];
id = json['id'];
name = json['name'];
email = json['email'];
body = json['body'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['postId'] = this.postId;
data['id'] = this.id;
data['name'] = this.name;
data['email'] = this.email;
data['body'] = this.body;
return data;
}
}
Thank you!
9
0
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line