Skip to content

Commit ada7069

Browse files
committed
add 中文文档
1 parent 5faf19b commit ada7069

File tree

2 files changed

+166
-1
lines changed

2 files changed

+166
-1
lines changed

README-ZH.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
2+
语言: [English](README.md) | [中文简体](README-ZH.md)
3+
4+
5+
# json_model [![Pub](https://img.shields.io/pub/v/json_model.svg?style=flat-square)](https://pub.dartlang.org/packages/json_model)
6+
7+
只用一行命令,直接将Json文件转为Dart model类。
8+
9+
## 安装
10+
11+
```yaml
12+
dev_dependencies:
13+
json_model: #最新版本
14+
```
15+
16+
## Getting Started
17+
18+
1. 在工程根目录下创建一个名为 "jsons" 的目录;
19+
2. 创建或拷贝Json文件到"jsons" 目录中 ;
20+
3. 运行 `pub run json_model` (Dart VM工程)or `flutter packages pub run json_model`(Flutter中) 命令生成Dart model类,生成的文件默认在"lib/models"目录下
21+
22+
## 例子
23+
24+
Json文件: `jsons/user.json`
25+
26+
```javascript
27+
{
28+
"name":"wendux",
29+
"father":"$user", //可以通过"$"符号引用其它model类
30+
"friends":"$[]user", // 可以通过"$[]"来引用数组
31+
"keywords":"$[]String", // 同上
32+
"age":20
33+
}
34+
```
35+
36+
生成的Dart model类:
37+
38+
```dart
39+
import 'package:json_annotation/json_annotation.dart';
40+
part 'user.g.dart';
41+
42+
@JsonSerializable()
43+
class User {
44+
User();
45+
46+
String name;
47+
User father;
48+
List<User> friends;
49+
List<String> keywords;
50+
num age;
51+
52+
factory User.fromJson(Map<String,dynamic> json) => _$UserFromJson(json);
53+
Map<String, dynamic> toJson() => _$UserToJson(this);
54+
}
55+
56+
```
57+
58+
### @JsonKey
59+
60+
您也可以使用[json_annotation](https://pub.dev/packages/json_annotation)包中的“@JsonKey”标注特定的字段。
61+
62+
这个功能在特定场景下非常有用,比如Json文件中有一个字段名为"+1",由于在转成Dart类后,字段名会被当做变量名,但是在Dart中变量名不能包含“+”,我们可以通过“@JsonKey”来映射变量名;
63+
64+
```javascript
65+
{
66+
"@JsonKey(ignore: true) dynamic":"md",
67+
"@JsonKey(name: '+1') int": "loved", //将“+1”映射为“loved”
68+
"name":"wendux",
69+
"age":20
70+
}
71+
```
72+
73+
生成文件如下:
74+
75+
```dart
76+
import 'package:json_annotation/json_annotation.dart';
77+
part 'user.g.dart';
78+
79+
@JsonSerializable()
80+
class User {
81+
User();
82+
@JsonKey(name: '+1') int loved;
83+
String name;
84+
num age;
85+
86+
factory User.fromJson(Map<String,dynamic> json) => _$UserFromJson(json);
87+
Map<String, dynamic> toJson() => _$UserToJson(this);
88+
}
89+
```
90+
91+
测试:
92+
93+
```dart
94+
import 'models/index.dart';
95+
96+
void main() {
97+
var u = User.fromJson({"name": "Jack", "age": 16, "+1": 20});
98+
print(u.loved); // 20
99+
}
100+
```
101+
102+
> 关于 `@JsonKey`标注的详细内容请参考[json_annotation](https://pub.dev/packages/json_annotation) 包;
103+
104+
### @Import
105+
106+
另外,提供了一个`@Import `指令,该指令可以在生成的Dart类中导入指定的文件:
107+
108+
```json
109+
{
110+
"@import":"test_dir/profile.dart",
111+
"@JsonKey(ignore: true) Profile":"profile",
112+
"name":"wendux",
113+
"age":20
114+
}
115+
```
116+
117+
生成的Dart类:
118+
119+
```dart
120+
import 'package:json_annotation/json_annotation.dart';
121+
import 'test_dir/profile.dart'; // 指令生效
122+
part 'user.g.dart';
123+
124+
@JsonSerializable()
125+
class User {
126+
User();
127+
128+
@JsonKey(ignore: true) Profile profile; //file
129+
String name;
130+
num age;
131+
132+
factory User.fromJson(Map<String,dynamic> json) => _$UserFromJson(json);
133+
Map<String, dynamic> toJson() => _$UserToJson(this);
134+
}
135+
```
136+
137+
更完整的示例请移步[这里](https://github.com/flutterchina/json_model/tree/master/example) .
138+
139+
## 命令参数
140+
141+
默认的源json文件目录为根目录下名为 "json" 的目录;可以通过 `src` 参数自定义源json文件目录,例如:
142+
143+
```shell
144+
pub run json_model src=json_files
145+
```
146+
147+
默认的生成目录为"lib/models",同样也可以通过`dist` 参数来自定义输出目录:
148+
149+
```shell
150+
pub run json_model src=json_files dist=data # 输出目录为 lib/data
151+
```
152+
153+
> 注意,dist会默认已lib为根目录。
154+
155+
## 代码调用
156+
157+
如果您正在开发一个工具,想在代码中使用json_model,此时便不能通过命令行来调用json_model,这是你可以通过代码调用:
158+
159+
```dart
160+
import 'package:json_model/json_model.dart';
161+
void main() {
162+
run(['src=jsons']); //run方法为json_model暴露的方法;
163+
}
164+
```
165+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dev_dependencies:
2323

2424
File: `jsons/user.json`
2525

26-
```json
26+
```javascript
2727
{
2828
"name":"wendux",
2929
"father":"$user", //Other class model

0 commit comments

Comments
 (0)