简体   繁体   English

如何在颤振中创建 JSON 对象?

[英]How to create JSON object in flutter?

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.我正在开发一个应用程序,我想在其中填写注册表并将此数据传递给 API。我尝试了几乎所有解决方案,但没有解决此问题。

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 = []; var fieldsData = []; final myControllers = [];最终 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.这是我的方法,我知道这个问题是由于 for 循环而发生的,但没有 for 循环我无法获取所有字段数据。 so please help.所以请帮忙。

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.由于您有一个对象列表,因此可能需要在列表中为每个项目调用 json.encode,然后对结果进行编码。

We convert many kind of Objects and List to JSON string in Dart/Flutter.我们在 Dart/Flutter 中将多种对象和列表转换为 JSON 字符串。 One of the most important part to make our work simple is the dart:convert library's built-in jsonEncode() function.使我们的工作变得简单的最重要的部分之一是dart:convert库的内置jsonEncode()函数。

Here We use map on tags property to return the JSON object.这里我们使用tags属性上的map来返回 JSON 对象。 Then, convert the map result to a List of JSON object using toList() method.然后,使用toList()方法将地图结果转换为JSON object List

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.让我们在控制台中检查结果,您将看到 JSON 字符串。

{"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.如果我们不使用作者和标签初始化 Tutorial 如下代码。

import 'dart:convert';

main() {
  Tutorial tutorial = Tutorial('Dart Tut#3', 'Tut#3 Description');
  String jsonTutorial = jsonEncode(tutorial);
  print(jsonTutorial);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM