To parse JSON in Dart or any language is simple. If you are a lazy programmer, you can generate codes for parsing JSON in any language using the following simple online tools.

Here is an example of a JSON file want to parse in my Flutter application.

{ "audio_link":"../audio/", "image_link":"../image/", "prayer":[ { "id":"1", "en":"Oṃ! Homage to the Buddha, Dharma and Noble Saṅgha—", "ro":"om, nangsi namdak rangshyin lhündrubpé" }, { "id":"2", "en":"All that dwell in the auspicious realms of the ten directions,", "ro":"tashi chok chü shying na shyukpa yi" } ] }
Code language: JSON / JSON with Comments (json)

https://app.quicktype.io/

Go to the URL given above, give a name to your parser class, insert your JSON content and the platform will generate code for you.

Given below is a screenshot of an example.

Parse JSON in Dart

Code Generated Sample

// To parse this JSON data, do // // final transLation = transLationFromJson(jsonString); import 'dart:convert'; TransLation transLationFromJson(String str) => TransLation.fromJson(json.decode(str)); String transLationToJson(TransLation data) => json.encode(data.toJson()); class TransLation { TransLation({ this.audioLink, this.imageLink, this.prayer, }); String audioLink; String imageLink; List<Prayer> prayer; factory TransLation.fromJson(Map<String, dynamic> json) => TransLation( audioLink: json["audio_link"], imageLink: json["image_link"], prayer: List<Prayer>.from(json["prayer"].map((x) => Prayer.fromJson(x))), ); Map<String, dynamic> toJson() => { "audio_link": audioLink, "image_link": imageLink, "prayer": List<dynamic>.from(prayer.map((x) => x.toJson())), }; } class Prayer { Prayer({ this.id, this.en, this.ro, }); String id; String en; String ro; factory Prayer.fromJson(Map<String, dynamic> json) => Prayer( id: json["id"], en: json["en"], ro: json["ro"], ); Map<String, dynamic> toJson() => { "id": id, "en": en, "ro": ro, }; }
Code language: JavaScript (javascript)

Another platform you may try is https://javiercbk.github.io/json_to_dart/.

Leave a Reply

Scroll to Top