convert string to json dart code example

Example 1: flutter access json object inside object

myJson = {
  "label": "This is a string",
  "value": {
  	"address": "This is a string",
    "country": "This is a string"
  }
}

//to access address field
var decodedJson = json.decode(myJson);
var jsonValue = json.decode(decodedJson['value']);
print(jsonValue['address']);

Example 2: flutter convert json string to json

flutter convert json string to json
----------------------
var x1 = '[{"id": "1"}]';
List x2 = jsonDecode(x1);
print(x2[0]);

Example 3: 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 json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    completed = json['completed'];
  }

  Map toJson() {
    final Map data = new Map();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['completed'] = this.completed;
    return data;
  }
}

Tags:

Misc Example