How to create JSON object in flutter?

8,382

Solution 1

Import:

import 'dart:convert';

then you can use:

json.encode(fieldsData);

As you have a list of objects, it may be necessary to make a for in the list calling json.encode for each item, then encoding the result.

Solution 2

We convert many kind of Objects and List to JSON string in Dart/Flutter. One of the most important part to make our work simple is the dart:convert library‘s built-in jsonEncode() function.

Here We use map on tags property to return the JSON object. Then, convert the map result to a List of JSON object using toList() method.

import 'dart:convert';

main() {
  User user = User('bezkoder', 21);
  String jsonUser = jsonEncode(user);
  print(jsonUser);

  List<Tag> tags = [Tag('tagA', 3), Tag('tagB', 6), Tag('tagC', 8)];
  String jsonTags = jsonEncode(tags);
  print(jsonTags);

  Tutorial tutorial = Tutorial('Dart Tut#2', 'Tut#2 Description', user, tags);
  String jsonTutorial = jsonEncode(tutorial);
  print(jsonTutorial);
}

Let’s check the result in console, you will see the JSON string.

{"title":"Dart Tut#2","description":"Tut#2 Description","author":{"name":"bezkoder","age":21},"tags":[{"name":"tagA","quantity":3},{"name":"tagB","quantity":6},{"name":"tagC","quantity":8}]}

If we don’t initialize Tutorial with author and tags as the following code.

import 'dart:convert';

main() {
  Tutorial tutorial = Tutorial('Dart Tut#3', 'Tut#3 Description');
  String jsonTutorial = jsonEncode(tutorial);
  print(jsonTutorial);
}
Share:
8,382
Shubham Solace
Author by

Shubham Solace

Updated on December 25, 2022

Comments

  • Shubham Solace
    Shubham Solace over 1 year

    I'm working on one application in which I want to fill the registration form and pass this data to API.I tried almost all solutions but not solved this issue.

    I'm getting this type of data after filling the form

    [
    {Date of Birth of the Baby: 08/01/1997},
    {Gender of the baby: male}, 
    {Time of Birth of the Baby: 12.00 pm}, 
    {Father's Name: Nnn}, 
    {Mother's Name: Hbhh}, 
    {Any Extra Details?: Bnn}
    ]
    

    and I want this type of data

     {
        "Date of Birth of the Baby": "08/01/1997",
        "Gender of the baby": "male",
        "Time of Birth of the Baby": "12.00 pm",
        "Father's Name": "Nnn",
        "Mother's Name": "Hbhh",
        "Any Extra Details?": "Bnn"
    }
    

    var fieldsData = []; final myControllers = [];

     mydynamicData() {
    
        for (var i = 0; i <= widget.fieldData.length; i++) {
          fieldsData.add({
            widget.fieldData[i]['question'],
            myControllers[i].text != null || myControllers[i].text != ""
                ? myControllers[i].text
                : ""
          });
    
        }
    
        print("fieldsData:${fieldsData}");
    
      }
    

    This is my method i know this issue occurred due to for loop but without for loop I'm not getting all fields data. so please help.

  • Shubham Solace
    Shubham Solace over 3 years
    [ {"Date of Birth of the Baby":"08/01/1996"}, {"Gender of the baby":""}, {"Time of Birth of the Baby":"12.99pm"}, {"Father's Name":"Nnn"}, {"Mother's Name":"Ggg"}, {"Any Extra Details?":"Bhhh"} ] it gives this type of data but I don't want curly braces like I want only [{ "" : "" }]
  • Shubham Solace
    Shubham Solace over 3 years
    please let me know if you have any idea
  • Luiz Filipe Medeira
    Luiz Filipe Medeira over 3 years
    you have a list of objects, you need to convert your list to a map.