[英]how to convert json string nested to json object in dart flutter?
the json string comes like this, json 字符串是这样的,
[[{"id":39,"mail":"mail@mm04.com","password":"q149","name":"Anthony","photo":"a14.png","dateac":"1900-01-01T18:36:36.000Z"},{"id":40,"mail":"mail@mm04.com","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}] [[{"id":39,"mail":"mail@mm04.com","password":"q149","name":"Anthony","photo":"a14.png","dateac" :"1900-01-01T18:36:36.000Z"},{"id":40,"mail":"mail@mm04.com","password":"q14","name":"Anthony", "照片":"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.我需要转换为两个对象:一方面是人员列表,另一方面是控制记录,我感谢任何指南,我是 dart 的新手,我已经尝试了几天。 Thanks.
谢谢。
This is the Object Model Of Your JSON data Now Call the http method and get the data via using this model.
class serverModel {
int id;
String mail;
String password;
String name;
String photo;
String dateac;
serverModel(
{this.id, this.mail, this.password, this.name, this.photo, this.dateac});
serverModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
mail = json['mail'];
password = json['password'];
name = json['name'];
photo = json['photo'];
dateac = json['dateac'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['mail'] = this.mail;
data['password'] = this.password;
data['name'] = this.name;
data['photo'] = this.photo;
data['dateac'] = this.dateac;
return data;
}
}
class FeeConcessionModel {
int id;
String mail;
String password;
String name;
String photo;
String dateac;
FeeConcessionModel(
{this.id, this.mail, this.password, this.name, this.photo, this.dateac});
FeeConcessionModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
mail = json['mail'];
password = json['password'];
name = json['name'];
photo = json['photo'];
dateac = json['dateac'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['mail'] = this.mail;
data['password'] = this.password;
data['name'] = this.name;
data['photo'] = this.photo;
data['dateac'] = this.dateac;
return data;
}
}
You can use given below website to convert the jaon to Dart object https://app.quicktype.io/您可以使用以下网站将 jaon 转换为 Dart object https://app.quicktype.io/
According to your JSON data, it has two parts: one is variable (List) and the another is fixed (Map).根据您的 JSON 数据,它有两个部分:一个是可变的(List),另一个是固定的(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/由https://app.quicktype.io/生成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.