Home > OS >  Read json in Flutter
Read json in Flutter

Time:04-08

Could anyone advise to read the JSON in Flutter as below:

{
   "1":{
      "title":"ករណី ANC",
      "lists":[
         {
            "id":"1135",
            "faci_code":"20103",
            "y_txt":"2022",
            "m_txt":"1",
            "ind_id":"1",
            "qty":"67",
            "rec_by":"od1234",
            "rec_date":"2022-03-02 13:53:58",
            "lock_txt":"0",
            "sec_id":"1",
            "ind_num":"1.0",
            "ind_eng":"# of ANC 1",
            "ind_kh":"ចំនួនស្រ្ដីបានពិនិត្យផ្ទៃពោះលើកទី១ទាំងអស់",
            "HFAC_NAME":"Rung Chrey",
            "HFAC_NAMEKh":"រូងជ្រៃ",
            "OD_CODE":"201",
            "OD_NAME":"Thma Koul",
            "OD_NAME_KH":"ថ្មគោល",
            "PRO_CODE":"2",
            "PROVINCE":"Battambang",
            "PROVINCE_KH":"បាត់ដំបង"
         },
         {
            "id":"1136",
            "faci_code":"20103",
            "y_txt":"2022",
            "m_txt":"1",
            "ind_id":"2",
            "qty":"32",
            "rec_by":"od1234",
            "rec_date":"2022-03-02 13:53:58",
            "lock_txt":"0",
            "sec_id":"1",
            "ind_num":"1.1",
            "ind_eng":"# of ANC 2",
            "ind_kh":"ចំនួនស្រ្ដីបានពិនិត្យផ្ទៃពោះលើកទី២ទាំងអស់",
            "HFAC_NAME":"Rung Chrey",
            "HFAC_NAMEKh":"រូងជ្រៃ",
            "OD_CODE":"201",
            "OD_NAME":"Thma Koul",
            "OD_NAME_KH":"ថ្មគោល",
            "PRO_CODE":"2",
            "PROVINCE":"Battambang",
            "PROVINCE_KH":"បាត់ដំបង"
         }
      ]
   }
}

Regard, thanks. Vandet

CodePudding user response:

import dart:convert;

And then

Map<String, dynamic> dataObject = jsonDecode(yourJsonData);

See the docs: https://docs.flutter.dev/development/data-and-backend/json

CodePudding user response:

From my interpretation of your question, you either want to directly access the above json in Flutter OR you want to create models out of it.

  1. In case you want to simply use the above Json in flutter, you can simply access its values using the keys. For example: assume the above json is stored in a variable jsonString and you want to print the first list and the first element of the lists under it -

print(jsonString["1"]);
print(jsonString["1"]["lists"][0]);

  1. In case you want to create models out of the above json, you can either do it yourself by defining encompassing objects as separate classes and going around with it OR you can use some online tool to assist you in the same, like https://javiercbk.github.io/json_to_dart/, this will create an entire converion model for you from the sample json. In case of above json it would be like -

class Autogenerated {
  One? one;

  Autogenerated({this.one});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    one = json['One'] != null ? new One.fromJson(json['One']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.one != null) {
      data['One'] = this.one!.toJson();
    }
    return data;
  }
}

class One {
  String? title;
  List<Lists>? lists;

  One({this.title, this.lists});

  One.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    if (json['lists'] != null) {
      lists = <Lists>[];
      json['lists'].forEach((v) {
        lists!.add(new Lists.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    if (this.lists != null) {
      data['lists'] = this.lists!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Lists {
  String? id;
  String? faciCode;
  String? yTxt;
  String? mTxt;
  String? indId;
  String? qty;
  String? recBy;
  String? recDate;
  String? lockTxt;
  String? secId;
  String? indNum;
  String? indEng;
  String? indKh;
  String? hFACNAME;
  String? hFACNAMEKh;
  String? oDCODE;
  String? oDNAME;
  String? oDNAMEKH;
  String? pROCODE;
  String? pROVINCE;
  String? pROVINCEKH;

  Lists(
      {this.id,
      this.faciCode,
      this.yTxt,
      this.mTxt,
      this.indId,
      this.qty,
      this.recBy,
      this.recDate,
      this.lockTxt,
      this.secId,
      this.indNum,
      this.indEng,
      this.indKh,
      this.hFACNAME,
      this.hFACNAMEKh,
      this.oDCODE,
      this.oDNAME,
      this.oDNAMEKH,
      this.pROCODE,
      this.pROVINCE,
      this.pROVINCEKH});

  Lists.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    faciCode = json['faci_code'];
    yTxt = json['y_txt'];
    mTxt = json['m_txt'];
    indId = json['ind_id'];
    qty = json['qty'];
    recBy = json['rec_by'];
    recDate = json['rec_date'];
    lockTxt = json['lock_txt'];
    secId = json['sec_id'];
    indNum = json['ind_num'];
    indEng = json['ind_eng'];
    indKh = json['ind_kh'];
    hFACNAME = json['HFAC_NAME'];
    hFACNAMEKh = json['HFAC_NAMEKh'];
    oDCODE = json['OD_CODE'];
    oDNAME = json['OD_NAME'];
    oDNAMEKH = json['OD_NAME_KH'];
    pROCODE = json['PRO_CODE'];
    pROVINCE = json['PROVINCE'];
    pROVINCEKH = json['PROVINCE_KH'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['faci_code'] = this.faciCode;
    data['y_txt'] = this.yTxt;
    data['m_txt'] = this.mTxt;
    data['ind_id'] = this.indId;
    data['qty'] = this.qty;
    data['rec_by'] = this.recBy;
    data['rec_date'] = this.recDate;
    data['lock_txt'] = this.lockTxt;
    data['sec_id'] = this.secId;
    data['ind_num'] = this.indNum;
    data['ind_eng'] = this.indEng;
    data['ind_kh'] = this.indKh;
    data['HFAC_NAME'] = this.hFACNAME;
    data['HFAC_NAMEKh'] = this.hFACNAMEKh;
    data['OD_CODE'] = this.oDCODE;
    data['OD_NAME'] = this.oDNAME;
    data['OD_NAME_KH'] = this.oDNAMEKH;
    data['PRO_CODE'] = this.pROCODE;
    data['PROVINCE'] = this.pROVINCE;
    data['PROVINCE_KH'] = this.pROVINCEKH;
    return data;
  }
}

NOTE: Avoid using numbers as json keys.

  • Related