Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

how to convert json string nested to json object in dart flutter?

the json string comes like this ,

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

I need to convert to two objects: on the one hand a list of people and the other a control record, I appreciate any guide I am new to dart and I have been trying for several days. Thanks.

question from:https://stackoverflow.com/questions/65930925/how-to-convert-json-string-nested-to-json-object-in-dart-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to your JSON data, it has two parts: one is variable (List) and the another is fixed (Map). So you need to think how to deal with that structure, one example:

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

Respect to classes to use:

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,
    };
}

Generated by https://app.quicktype.io/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...