文档
import 'dart:convert';
main() async {
// json 转化为 map
String jsonStr = '''
[
{"name": "ajanuw"},
{"name": "suou"}
]
''';
var jsonObj = jsonDecode(jsonStr);
print(jsonObj[0]['name'] == 'ajanuw'); // true
// list
使用对象添加模板 JSON to Dart
import 'dart:convert';
class MyInfo {
MyInfo({this.age, this.name});
String name;
int age;
factory MyInfo.fromJson(Map jsonMap) {
return MyInfo(
name: jsonMap['name'],
age: jsonMap['age'],
);
}
}
main(List args) {
String jsonStr = '''
{
"name": "ajanuw",
"age": 12
}
''';
var parsed = jsonDecode(jsonStr);
MyInfo data = MyInfo.fromJson(parsed);
print(data.age);
}
使用“built_value”库来处理json
- pub库
- video教程
- 转换器
我在这里使用"stagehand console-full"创建的项目示例,项目名叫"dart_test"
1) 安装依赖
dependencies:
...
built_value:
built_collection:
dev_dependencies:
...
build_runner:
built_value_generator:
3) 准备json数据
{
"id": 157538,
"date": "2017-07-21T10:30:34",
"date_gmt": "2017-07-21T17:30:34",
"type": "post",
"link": "https://example.com",
"title": {
"rendered": "Json 2 dart built_value converter"
},
"tags": [
1798,
6298
]
}
3) 创建文件"lib/models/hello.dart",文件名很重要必须要要和你的class名一样
import 'dart:convert';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'hello.g.dart';
abstract class Hello implements Built {
Hello._();
factory Hello([updates(HelloBuilder b)]) = _$Hello;
@BuiltValueField(wireName: 'id')
int get id;
@BuiltValueField(wireName: 'date')
String get date;
@BuiltValueField(wireName: 'date_gmt')
String get dateGmt;
@BuiltValueField(wireName: 'type')
String get type;
@BuiltValueField(wireName: 'link')
String get link;
@BuiltValueField(wireName: 'title')
Title get title;
@BuiltValueField(wireName: 'tags')
BuiltList get tags;
String toJson() {
return jsonEncode(serializers.serializeWith(Hello.serializer, this));
}
static Hello fromJson(String jsonString) {
return serializers.deserializeWith(
Hello.serializer, jsonDecode(jsonString));
}
static Serializer get serializer => _$helloSerializer;
}
abstract class Title implements Built {
Title._();
factory Title([updates(TitleBuilder b)]) = _$Title;
@BuiltValueField(wireName: 'rendered')
String get rendered;
String toJson() {
return jsonEncode(serializers.serializeWith(Title.serializer, this));
}
static Title fromJson(String jsonString) {
return serializers.deserializeWith(
Title.serializer, jsonDecode(jsonString));
}
static Serializer get serializer => _$titleSerializer;
}
4) 保存文件后,在项目根目录下运行命令, 生成"lib/models/hello.g.dart"文件,watch: 监听文件的变化
> flutter packages pub run build_runner watch
5) 创建"lib/models/serializers.dart"文件,编写下面的代码后,保存会生成"lib/models/serializers.g.dart"文件
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:dart_test/models/hello.dart';
part 'serializers.g.dart';
@SerializersFor(const [
Hello
])
final Serializers serializers = (_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
6) 编写“bin/main.dart"
import 'package:dart_test/models/hello.dart';
String jsonStr = '''
{
"id": 157538,
"date": "2017-07-21T10:30:34",
"date_gmt": "2017-07-21T17:30:34",
"type": "post",
"link": "https://example.com",
"title": {
"rendered": "Json 2 dart built_value converter"
},
"tags": [
1798,
6298
]
}
''';
main(List args) {
Hello hello = Hello.fromJson(jsonStr);
print(hello.title.rendered);
print(hello.tags.length);
}
7) 保存文件后执行
> dart bin/main.dart
Json 2 dart built_value converter
2