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,
};
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句