如何在dart flutter中将嵌套的json字符串转换为json对象?

阿曼多

json字符串是这样的,

[[{{id“:39,” mail“:” [email protected]“,” password“:” q149“,” name“:” Anthony“,” photo“:” a14.png“,” dateac“ :“” 1900-01-01T18:36:36.000Z“},{” id“:40,” mail“:” [email protected]“,” password“:” q14“,” name“:” Anthony“, “ photo”:“ a3.png”,“ dateac”:“ 1900-01-01T18:36:36.000Z”}],{“ fieldCount”:0,“ affectedRows”:0,“ insertId”:0,“ serverStatus “:34,” warningCount“:0:” message“:”“,” protocol41“:true,” changedRows“:0}]]

我需要转换为两个对象:一方面是人员清单,另一方面是控制记录,我很感激我对飞镖不熟悉的任何指南,并且已经尝试了好几天。谢谢。

威廉·阿奇亚

根据您的JSON数据,它分为两部分:一个是变量(列表),另一个是固定的(地图)。因此,您需要考虑如何处理该结构,例如一个例子:

List<Object> : L
    L[0] : List<Object> : K
        K[i] : Map<String, Object> : where i = 0.. K.length-1
            - convert every element to an instance of Person class
    L[1] : Map<String, Object>
        - convert this element to an instance of Control class

尊重使用的类:

import 'package:meta/meta.dart';
import 'dart:convert';

class Person {
    Person({
        @required this.id,
        @required this.mail,
        @required this.password,
        @required this.name,
        @required this.photo,
        @required this.dateac,
    });

    int id;
    String mail;
    String password;
    String name;
    String photo;
    DateTime dateac;

    factory Person.fromRawJson(String str) => Person.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Person.fromJson(Map<String, dynamic> json) => Person(
        id: json["id"],
        mail: json["mail"],
        password: json["password"],
        name: json["name"],
        photo: json["photo"],
        dateac: DateTime.parse(json["dateac"]),
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "mail": mail,
        "password": password,
        "name": name,
        "photo": photo,
        "dateac": dateac.toIso8601String(),
    };
}

class Control {
    Control({
        @required this.fieldCount,
        @required this.affectedRows,
        @required this.insertId,
        @required this.serverStatus,
        @required this.warningCount,
        @required this.message,
        @required this.protocol41,
        @required this.changedRows,
    });

    int fieldCount;
    int affectedRows;
    int insertId;
    int serverStatus;
    int warningCount;
    String message;
    bool protocol41;
    int changedRows;

    factory Control.fromRawJson(String str) => Control.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Control.fromJson(Map<String, dynamic> json) => Control(
        fieldCount: json["fieldCount"],
        affectedRows: json["affectedRows"],
        insertId: json["insertId"],
        serverStatus: json["serverStatus"],
        warningCount: json["warningCount"],
        message: json["message"],
        protocol41: json["protocol41"],
        changedRows: json["changedRows"],
    );

    Map<String, dynamic> toJson() => {
        "fieldCount": fieldCount,
        "affectedRows": affectedRows,
        "insertId": insertId,
        "serverStatus": serverStatus,
        "warningCount": warningCount,
        "message": message,
        "protocol41": protocol41,
        "changedRows": changedRows,
    };
}

https://app.quicktype.io/生成

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何正确将json字符串转换为对象

如何在golang中将字符串转换为json?

如何在Go中将字符串转换为JSON?

如何在golang中将动态生成的数组对象数据转换为JSON格式的字符串?

如何将此嵌套的json字符串转换为Java对象?

Dart对象-> JSON字符串无法转换为JSON

如何在JavaScript中将字符串转换为JSON对象

如何在Bash中将字符串列表转换为JSON字符串数组?

如何在json中拆分字符串值并使用jq转换为嵌套对象?

如何在dart flutter中将json字符串转换为json对象?

如何在Flutter中将颜色转换为字符串?

如何在Angular中将字符串转换为json?

如何在Javascript中将字符串转换为对象?

如何在Flutter中将字符串转换为对象?

如何在播放json中将嵌套的Json字符串转换为Json结构

如何在Unity3D中将类对象转换为JSon字符串?

如何在Android中将JSON对象转换为字符串

如何在php中将JSON对象转换为纯逗号分隔的字符串?

如何通过例如将嵌套对象转换为JSON中的JSON字符串来防止嵌套JSON?

如何在 Ballerina 中将 json 转换为字符串?

如何在 JavaScript 中将字符串转换为对象?

如何在 Reactjs 中将嵌套的对象数组转换为字符串数组?

如何在 dart:flutter 中将 Future<dynamic> 转换为字符串

如何在 C# 中将嵌套的 json 字符串转换为可访问的结构以打印选择项?

如何在hive中将字符串数组转换为json字符串?

如何在flutter中将二进制字符串转换为json字符串

如何在c#中将json格式字符串转换为json

如何在 dart 中将 x-www-form-urlencoded 字符串转换为 json?

如何在飞镖颤振中将json嵌套字符串转换为json