【问题标题】:read json data with two arrays flutter用两个数组flutter读取json数据
【发布时间】:2021-01-21 17:37:22
【问题描述】:

你好,我是 Flutter 的新手,我正在构建一个需要解析 json 数据的应用程序是 json 的代码:

{
   "folders":[
      {
         "id":2107,
         "name":"Labs ",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-09-30 03:00:39",
         "inarchive":0,
         "active":1
      },
      {
         "id":2108,
         "name":"L1",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-09-30 03:00:45",
         "inarchive":0,
         "active":1
      },
      {
         "id":2130,
         "name":"L2",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-02
02:39:11",
         "inarchive":0,
         "active":1
      },
      {
         "id":2175,
         "name":"L3",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-10-07 03:43:15",
         "inarchive":0,
         "active":1
      },
      {
         "id":2202,
         "name":"L4",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-09 02:08:10",
         "inarchive":0,
         "active":1
      },
      {
         "id":2227,
         "name":"\u0645\u0631\u0627\u062c\u0639\u0627\u062a
",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-10-11 05:15:43",
         "inarchive":0,
         "active":1
      },
      {
         "id":2242,
         "name":"L5",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-14 03:45:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2257,
         "name":"L6",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-16
06:23:41",
         "inarchive":0,
         "active":1
      },
      {
         "id":2269,
         "name":"L7",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-21 03:39:12",
         "inarchive":0,
         "active":1
      },
      {
         "id":2309,
         "name":"L8",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-23 05:14:48",
         "inarchive":0,
         "active":1
      },
      {
         "id":2310,
         "name":"L8",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-23
05:14:51",
         "inarchive":0,
         "active":1
      },
      {
         "id":2346,
         "name":"L9",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-28 04:22:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2370,
         "name":"L10",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-30 03:02:00",
         "inarchive":0,
         "active":1
      },
      {
         "id":2446,
         "name":"L11",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-12
06:28:13",
         "inarchive":0,
         "active":1
      },
      {
         "id":2462,
         "name":"\u062a\u0628\u064a\u0636\u0627\u062a",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-11-13 08:39:00",
         "inarchive":0,
         "active":1
      },
      {
         "id":2464,
         "name":"L12",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-13
08:40:55",
         "inarchive":0,
         "active":1
      },
      {
         "id":2488,
         "name":"L13",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18 06:09:11",
         "inarchive":0,
         "active":1
      },
      {
         "id":2490,
         "name":"L14",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18 06:38:59",
         "inarchive":0,
         "active":1
      },
      {
         "id":2491,
         "name":"L15",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18
06:45:57",
         "inarchive":0,
         "active":1
      },
      {
         "id":2497,
         "name":"\u062a\u0644\u062e\u064a\u0635\u0627\u062a",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-11-19 04:24:49",
         "inarchive":0,
         "active":1
      },
      {
         "id":2509,
         "name":"L16",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-20
09:56:04",
         "inarchive":0,
         "active":1
      },
      {
         "id":2522,
         "name":"L17",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-25 06:38:27",
         "inarchive":0,
         "active":1
      },
      {
         "id":2542,
         "name":"L18",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-27 12:19:12",
         "inarchive":0,
         "active":1
      },
      {
         "id":2552,
         "name":"Protein
Lab",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-27 16:32:27",
         "inarchive":0,
         "active":1
      },
      {
         "id":2562,
         "name":"Chromatography ",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-30 15:24:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2576,
         "name":"L19",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-02
02:05:29",
         "inarchive":0,
         "active":1
      },
      {
         "id":2580,
         "name":"L20",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-02 08:23:48",
         "inarchive":0,
         "active":1
      },
      {
         "id":2592,
         "name":"21",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-03 06:27:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2600,
         "name":"L22",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-04
06:22:46",
         "inarchive":0,
         "active":1
      },
      {
         "id":2614,
         "name":"L23",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-04 10:03:00",
         "inarchive":0,
         "active":1
      },
      {
         "id":2640,
         "name":"L24",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-08 06:53:11",
         "inarchive":0,
         "active":1
      },
      {
         "id":2644,
         "name":"L25",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-09
05:33:07",
         "inarchive":0,
         "active":1
      },
      {
         "id":2649,
         "name":"L26",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-09 07:58:42",
         "inarchive":0,
         "active":1
      },
      {
         "id":2711,
         "name":"\u0623\u0633\u0626\u0644\u0629",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-19
05:18:11",
         "inarchive":0,
         "active":1
      }
   ],
   "files":[
      {
         "id":4542,
         "name":"Ch2-part1.m4a 27.52MB",
         "img":null,
         "uploader":"Aws",
         "url":"http:\/\/msc-mu.com\/..\/uploaded\/031020190Ch2-part1.m4a",
         "createdate":"2019-10-03 09:53:39",
         "approved":1,
         "active":1
      }
   ]
}

我使用的是 mysql 数据库,这是格式化的 json 代码。

【问题讨论】:

    标签: mysql json flutter


    【解决方案1】:

    由于您还没有编写任何我们可以评论的代码,所以我只是总结一个建议。

    实现这一点的一种方法是将包 json_serializable 与 build_runner 一起使用,并将其与几个类(例如,名为 Folder 和 FolderList)一起使用,这些类应该保存与文件夹和文件夹列表关联的值。

    然后让 json_serializable 和 build_runner 生成解析到和从 json 解析的方法。

    对于带有示例的文档,这是一个很好的阅读资源: https://flutter.dev/docs/development/data-and-backend/json

    【讨论】:

    • 你好,我知道我还没有实现任何代码,但这就是为什么我发布了我厌倦的一切都没有用的问题!
    • 我可以联系你吗?
    • @FahmiSawalha 我添加了一个很好的资源链接,其中包含有关如何将 json 与颤振一起使用的示例。这应该为您指明正确的方向。
    【解决方案2】:

    首先,您必须为您的文件夹对象创建一个模型

    class Folder {
         String id;
         String name;
         String img;
         // etc...
    // Create constructor
    // json converted
    factory Folder.fromJson(Map<String, dynamic> json) {
    return Folder(
      id: json['id'],
      name: json['name'],
      image: json['image'],
    );
    

    }

    其次,可以做到以下几点

    List<Folder> folders = (json['folders'] as List).map((folder) => Folder.fromJson(folder)).toList();
    

    【讨论】:

    • 我应该在哪里做第二步?
    • Future&lt;List&lt;Folder&gt;&gt;fellowSubject() async{ final jsonEndpoint = "http://msc-mu.com/api_verfication.php"; final response = await http.post(jsonEndpoint,body: { 'flag':'selectfellowsubjects', 'parentsubject':{widget.value.id} }); if (response.statusCode == 200) { List&lt;Folder&gt; folders = (json[] as List).map((folder) =&gt; Folder.fromJson(folder)).toList(); return folders; } else throw Exception('We were not able to successfully download the Main Subjects.'); }
    • 是的,但是 (json['folders'] as List).map 不是 (json[] as List).map
    • 你能给我你的不和谐吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2019-03-26
    • 2020-05-05
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多