dart string to json code example
Example 1: flutter generate json files
flutter packages pub run build_runner build
Example 2: 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);
Example 3: flutter convert json string to json
flutter convert json string to json
----------------------
var x1 = '[{"id": "1"}]';
List<dynamic> x2 = jsonDecode(x1);
print(x2[0]);
Example 4: convert json to dart
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;
}
}