1import 'dart:convert';
2
3main() {
4 String nestedObjText =
5 '{"title": "Dart Tutorial", "description": "Way to parse Json", "author": {"name": "bezkoder", "age": 30}}';
6
7 Tutorial tutorial = Tutorial.fromJson(jsonDecode(nestedObjText));
8
9 print(tutorial);
10
1class Autogenerated {
2 int userId;
3 int id;
4 String title;
5 String body;
6
7 Autogenerated({this.userId, this.id, this.title, this.body});
8
9 Autogenerated.fromJson(Map<String, dynamic> json) {
10 userId = json['userId'];
11 id = json['id'];
12 title = json['title'];
13 body = json['body'];
14 }
15
16 Map<String, dynamic> toJson() {
17 final Map<String, dynamic> data = new Map<String, dynamic>();
18 data['userId'] = this.userId;
19 data['id'] = this.id;
20 data['title'] = this.title;
21 data['body'] = this.body;
22 return data;
23 }
24}
25
1class Todo {
2 int userId;
3 int id;
4 String title;
5 bool completed;
6
7 Todo({this.userId, this.id, this.title, this.completed});
8
9 Todo.fromJson(Map<String, dynamic> json) {
10 userId = json['userId'];
11 id = json['id'];
12 title = json['title'];
13 completed = json['completed'];
14 }
15
16 Map<String, dynamic> toJson() {
17 final Map<String, dynamic> data = new Map<String, dynamic>();
18 data['userId'] = this.userId;
19 data['id'] = this.id;
20 data['title'] = this.title;
21 data['completed'] = this.completed;
22 return data;
23 }
24}
25
1class posts {
2 int idPost;
3 String datePost;
4 String objectPost;
5 String contenuPost;
6 double latitude;
7 double longitude;
8 List<Null> fichiers;
9 Null domaine;
10 List<Null> partages;
11 Null user;
12 List<Null> commentaireCorpsMetiers;
13 Null post;
14 List<Null> sousPost;
15 List<Null> votes;
16 List<Null> likes;
17
18 posts(
19 {this.idPost,
20 this.datePost,
21 this.objectPost,
22 this.contenuPost,
23 this.latitude,
24 this.longitude,
25 this.fichiers,
26 this.domaine,
27 this.partages,
28 this.user,
29 this.commentaireCorpsMetiers,
30 this.post,
31 this.sousPost,
32 this.votes,
33 this.likes});
34
35 posts.fromJson(Map<String, dynamic> json) {
36 idPost = json['id_post'];
37 datePost = json['date_post'];
38 objectPost = json['object_post'];
39 contenuPost = json['contenu_post'];
40 latitude = json['latitude'];
41 longitude = json['longitude'];
42 if (json['fichiers'] != null) {
43 fichiers = new List<Null>();
44 json['fichiers'].forEach((v) {
45 fichiers.add(new Null.fromJson(v));
46 });
47 }
48 domaine = json['domaine'];
49 if (json['partages'] != null) {
50 partages = new List<Null>();
51 json['partages'].forEach((v) {
52 partages.add(new Null.fromJson(v));
53 });
54 }
55 user = json['user'];
56 if (json['commentaire_corpsMetiers'] != null) {
57 commentaireCorpsMetiers = new List<Null>();
58 json['commentaire_corpsMetiers'].forEach((v) {
59 commentaireCorpsMetiers.add(new Null.fromJson(v));
60 });
61 }
62 post = json['post'];
63 if (json['sous_post'] != null) {
64 sousPost = new List<Null>();
65 json['sous_post'].forEach((v) {
66 sousPost.add(new Null.fromJson(v));
67 });
68 }
69 if (json['votes'] != null) {
70 votes = new List<Null>();
71 json['votes'].forEach((v) {
72 votes.add(new Null.fromJson(v));
73 });
74 }
75 if (json['likes'] != null) {
76 likes = new List<Null>();
77 json['likes'].forEach((v) {
78 likes.add(new Null.fromJson(v));
79 });
80 }
81 }
82
83 Map<String, dynamic> toJson() {
84 final Map<String, dynamic> data = new Map<String, dynamic>();
85 data['id_post'] = this.idPost;
86 data['date_post'] = this.datePost;
87 data['object_post'] = this.objectPost;
88 data['contenu_post'] = this.contenuPost;
89 data['latitude'] = this.latitude;
90 data['longitude'] = this.longitude;
91 if (this.fichiers != null) {
92 data['fichiers'] = this.fichiers.map((v) => v.toJson()).toList();
93 }
94 data['domaine'] = this.domaine;
95 if (this.partages != null) {
96 data['partages'] = this.partages.map((v) => v.toJson()).toList();
97 }
98 data['user'] = this.user;
99 if (this.commentaireCorpsMetiers != null) {
100 data['commentaire_corpsMetiers'] =
101 this.commentaireCorpsMetiers.map((v) => v.toJson()).toList();
102 }
103 data['post'] = this.post;
104 if (this.sousPost != null) {
105 data['sous_post'] = this.sousPost.map((v) => v.toJson()).toList();
106 }
107 if (this.votes != null) {
108 data['votes'] = this.votes.map((v) => v.toJson()).toList();
109 }
110 if (this.likes != null) {
111 data['likes'] = this.likes.map((v) => v.toJson()).toList();
112 }
113 return data;
114 }
115}
116
1class Testing {
2 String guid;
3 int index;
4 String favoriteFruit;
5 double latitude;
6 String company;
7 String email;
8 String picture;
9 List<String> tags;
10 String registered;
11 String eyeColor;
12 String phone;
13 String address;
14 List<Friends> friends;
15 bool isActive;
16 String about;
17 String balance;
18 String name;
19 String gender;
20 int age;
21 String greeting;
22 double longitude;
23 String sId;
24
25 Testing(
26 {this.guid,
27 this.index,
28 this.favoriteFruit,
29 this.latitude,
30 this.company,
31 this.email,
32 this.picture,
33 this.tags,
34 this.registered,
35 this.eyeColor,
36 this.phone,
37 this.address,
38 this.friends,
39 this.isActive,
40 this.about,
41 this.balance,
42 this.name,
43 this.gender,
44 this.age,
45 this.greeting,
46 this.longitude,
47 this.sId});
48
49 Testing.fromJson(Map<String, dynamic> json) {
50 guid = json['guid'];
51 index = json['index'];
52 favoriteFruit = json['favoriteFruit'];
53 latitude = json['latitude'];
54 company = json['company'];
55 email = json['email'];
56 picture = json['picture'];
57 tags = json['tags'].cast<String>();
58 registered = json['registered'];
59 eyeColor = json['eyeColor'];
60 phone = json['phone'];
61 address = json['address'];
62 if (json['friends'] != null) {
63 friends = new List<Friends>();
64 json['friends'].forEach((v) {
65 friends.add(new Friends.fromJson(v));
66 });
67 }
68 isActive = json['isActive'];
69 about = json['about'];
70 balance = json['balance'];
71 name = json['name'];
72 gender = json['gender'];
73 age = json['age'];
74 greeting = json['greeting'];
75 longitude = json['longitude'];
76 sId = json['_id'];
77 }
78
79 Map<String, dynamic> toJson() {
80 final Map<String, dynamic> data = new Map<String, dynamic>();
81 data['guid'] = this.guid;
82 data['index'] = this.index;
83 data['favoriteFruit'] = this.favoriteFruit;
84 data['latitude'] = this.latitude;
85 data['company'] = this.company;
86 data['email'] = this.email;
87 data['picture'] = this.picture;
88 data['tags'] = this.tags;
89 data['registered'] = this.registered;
90 data['eyeColor'] = this.eyeColor;
91 data['phone'] = this.phone;
92 data['address'] = this.address;
93 if (this.friends != null) {
94 data['friends'] = this.friends.map((v) => v.toJson()).toList();
95 }
96 data['isActive'] = this.isActive;
97 data['about'] = this.about;
98 data['balance'] = this.balance;
99 data['name'] = this.name;
100 data['gender'] = this.gender;
101 data['age'] = this.age;
102 data['greeting'] = this.greeting;
103 data['longitude'] = this.longitude;
104 data['_id'] = this.sId;
105 return data;
106 }
107}
108
109class Friends {
110 int id;
111 String name;
112
113 Friends({this.id, this.name});
114
115 Friends.fromJson(Map<String, dynamic> json) {
116 id = json['id'];
117 name = json['name'];
118 }
119
120 Map<String, dynamic> toJson() {
121 final Map<String, dynamic> data = new Map<String, dynamic>();
122 data['id'] = this.id;
123 data['name'] = this.name;
124 return data;
125 }
126}
127