首页 > 解决方案 > 如何在颤振中创建 JSON 对象?

问题描述

我正在开发一个应用程序,我想在其中填写注册表并将这些数据传递给 API。我尝试了几乎所有的解决方案,但没有解决这个问题。

填写表格后我得到了这种类型的数据

[
{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}
]

我想要这种类型的数据

 {
    "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 字段数据 = []; 最终 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}");

  }

这是我的方法,我知道这个问题是由于 for 循环而发生的,但是没有 for 循环我没有得到所有字段数据。所以请帮忙。

标签: androidiosjsonflutterdart

解决方案


进口:

import 'dart:convert';

那么你可以使用:

json.encode(fieldsData);

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


推荐阅读