当前位置:网站首页>How does flutter use the online transcoding tool to convert JSON to model
How does flutter use the online transcoding tool to convert JSON to model
2022-06-24 22:04:00 【One leaf floating boat】
Domestic tycoon CrazyCodeBoy Tools provided :
Text begins .
The goal is json:
{
"posts": [
{
"id": "0",
"created": 1590453935992,
"content": " Offer based on GraphQL API Data query and access ,「Hasura」 a 990 Thousands of dollars A round ..."
},
{
"id": "1",
"created": 1590453935992,
"content": " Why? GraphQL yes API The future of "
},
{
"id": "2",
"created": 1590453935992,
"content": "Netflix: Why do we have to GraphQL Introduce front-end architecture ?"
}
]
}
open quicktype Website ( Scientific access to the web may be required ):Instantly parse JSON in any language | quicktype
Click on the top right corner Options Button , And make the following configuration :
Paste JSON Go to the input box , And enter the model name in the upper left corner PostsData
:
The right side will automatically generate the model :
Copy the code on the right , Create related types :
/lib/PostsData.dart:
// To parse this JSON data, do
//
// final postsData = postsDataFromJson(jsonString);
import 'dart:convert';
class PostsData {
final List<Post> posts;
PostsData({
this.posts,
});
factory PostsData.fromJson(String str) => PostsData.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory PostsData.fromMap(Map<String, dynamic> json) => PostsData(
posts: json["posts"] == null ? null : List<Post>.from(json["posts"].map((x) => Post.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"posts": posts == null ? null : List<dynamic>.from(posts.map((x) => x.toMap())),
};
}
class Post {
final String id;
final int created;
final String content;
Post({
this.id,
this.created,
this.content,
});
factory Post.fromJson(String str) => Post.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Post.fromMap(Map<String, dynamic> json) => Post(
id: json["id"] == null ? null : json["id"],
created: json["created"] == null ? null : json["created"],
content: json["content"] == null ? null : json["content"],
);
Map<String, dynamic> toMap() => {
"id": id == null ? null : id,
"created": created == null ? null : created,
"content": content == null ? null : content,
};
}
complete ,Good Job!
Other online conversion tools
One . Generate websites online :
https://javiercbk.github.io/json_to_dart/
Two . JsonToDart plug-in unit 【 recommend 】
stay Android Studio Install in JsonToDart plug-in unit ,
a. open Preferences(Mac) perhaps Setting(Window),
b. choice Plugins, Search for JsonToDart
Click on Install( The picture above has been installed ), After the installation is completed, restart
Select directory , Click on the right , choice New->Json to Dart
take json Copy the string in , Fill in the class description , Click on Generate button
With shortcut keys :
Windows:ALT + Shift + D
Mac:Option + Shift + D
3、 ... and . high efficiency JSON turn Model
- stay pubspec.yaml Add dependency to
json_annotation: ^3.1.0 json_serializable: ^3.5.0 build_runner: ^1.0.0
stay Android Stuido In the implementation of Pub get
- New model class (mode/demo_model.dart)
class DemoModel{
}
- Send the back end request to the web page JSON Data to Model:
https://czero1995.github.io/json-to-model/: Website transformation supports the transformation of complex objects with infinite levels
- For example, the following JSON Copy the data to the web page ( On the left ):
{
"code": 0,
"data": {
"avatar": "xxx.png",
"id": 7,
"float":0.1,
"is_deleted": false,
"nickname": "nickName",
"openId": null,
"phone": "13641418383",
"store_ids": [1,2],
"updated": "2020-11-05 11:53:10",
"more":[{"a":1,"b":"b","c":{"c1":0.2,"c2":2}}]
}
}
- And then convert to Model data ( On the right )
import 'package:json_annotation/json_annotation.dart';
part 'demo_model_data.g.dart';
@JsonSerializable(explicitToJson: true)
class DemoModelModel {
DemoModelData data;
DemoModelModel({
this.data,
this.code,
this.message
});
factory DemoModelModel.fromJson(Map<String, dynamic> json) => _$DemoModelModelFromJson(json);
Map<String, dynamic> toJson() => _$DemoModelModelToJson(this);
}
@JsonSerializable(explicitToJson: true)
class DemoModelData {
String avatar;
int id;
double float;
bool is_deleted;
String nickname;
var openId;
String phone;
List<int> store_ids;
String updated;
List<MoreData> more;
DemoModelData({
this.avatar,
this.id,
this.float,
this.is_deleted,
this.nickname,
this.openId,
this.phone,
this.store_ids,
this.updated,
this.more,
});
factory DemoModelData.fromJson(Map<String, dynamic> json) => _$DemoModelDataFromJson(json);
Map<String, dynamic> toJson() => _$DemoModelDataToJson(this);
}
@JsonSerializable(explicitToJson: true)
class MoreData{
int a;
String b;
CData c;
MoreData({
this.a,
this.b,
this.c,
});
factory MoreData.fromJson(Map<String, dynamic> json) => _$MoreDataFromJson(json);
Map<String, dynamic> toJson() => _$MoreDataToJson(this);
}
@JsonSerializable(explicitToJson: true)
class CData{
double c1;
int c2;
CData({
this.c1,
this.c2,
});
factory CData.fromJson(Map<String, dynamic> json) => _$CDataFromJson(json);
Map<String, dynamic> toJson() => _$CDataToJson(this);
}
After that, the data will be copied out and then covered demo_model.dart On the file
- perform build_runner
Execute the command in the project terminal :
flutter pub run build_runner build
After execution , Will generate demo_model.g.dart file
The whole execution process is as follows ( Open the link below to view )
https://image-static.segmentfault.com/305/532/3055326920-21e28cc64f409c11
Four . JSON to Dart Converter
JSON to Dart Converter - Convert JSON Code Online
边栏推荐
- After Firefox drag and drop, Baidu search will always be opened by default. If it is a picture, the picture will be opened.
- 拖动拖动拖动
- How to achieve energy conservation and environmental protection of full-color outdoor LED display
- [featured] how do you design unified login with multiple accounts?
- EasyBypass
- 手动事务的几个类
- Datakit agent realizes unified data aggregation in LAN
- Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
- LINQ query collection class introductory cases Wulin expert class
- You are using pip version 21.1.2; however, version 22.1.2 is available
猜你喜欢
最大流问题
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
leetcode:1504. 统计全 1 子矩形的个数
Li Kou daily question - day 26 -496 Next larger element I
心楼:华为运动健康的七年筑造之旅
权限想要细化到按钮,怎么做?
I really can't do it. After 00, I collapsed and wanted to leave
first-order-model实现照片动起来(附工具代码) | 机器学习
[theory] deep learning in the covid-19 epic: a deep model for urban traffic revitalization index
985测试工程师被吊打,学历和经验到底谁更重要?
随机推荐
leetcode_ 1470_ 2021.10.12
dp问题集
我国SaaS产业的发展趋势与路径
Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
C language - keyword 1
leetcode:55. 跳跃游戏【经典贪心】
Redis+Caffeine两级缓存,让访问速度纵享丝滑
优雅的自定义 ThreadPoolExecutor 线程池
The leader of ERP software in printing industry
Excel layout
心楼:华为运动健康的七年筑造之旅
Collective search + drawing creation
Réduire le PIP à la version spécifiée (mettre à jour le PIP avec pycharm avant de le réduire à la version originale)
[200 opencv routines] 209 Color image segmentation in HSV color space
PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘
机器学习:线性回归
I really can't do it. After 00, I collapsed and wanted to leave
印刷行业的ERP软件的领头羊
Notes on writing questions (18) -- binary tree: common ancestor problem
Practice of hierarchical management based on kubesphere