JSON Serialization in Dart

 JSON Serialization in Dart


JSON is a popular data format used for exchanging data between the client and server. Dart provides built-in support for parsing and serializing JSON data using the dart:convert package.


import 'dart:convert';


void main() {

  var jsonString = '{"name": "John Doe", "age": 25}';

  var jsonData = json.decode(jsonString);


  print(jsonData['name']); // Output: John Doe


  var person = {'name': 'Jane Smith', 'age': 30};

  var personJson = json.encode(person);


  print(personJson); // Output: {"name":"Jane Smith","age":30}

}

In the above example, we are using the json.decode method to parse a JSON string into a Map. We can access the values of the map using the keys.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...