在 Flutter 中将 JSON 转换为类似于 android 的 POJO(对象)
Posted
技术标签:
【中文标题】在 Flutter 中将 JSON 转换为类似于 android 的 POJO(对象)【英文标题】:Convert JSON into POJO (Object) similar to android in Flutter 【发布时间】:2019-01-24 20:56:24 【问题描述】:我只是想找到一种方法将 json 响应(来自 REST API)转换为 POJO(在 android 中使用),以便我可以将接收到的数据用于我的应用程序,因为使用 Map 是不够的我收到的数据有点复杂,包含 3 级数组(数组内数组内的数组)。
我正在使用 Dio 库进行 api 调用,我可以成功调用 API 并在控制台上打印数据,完全没有问题。谁能帮助我实现这一目标?
以下是我得到的回复。
"status": 200,
"message": "success",
"data": [
"menu":
"megamenu_id": "55",
"language_id": "1",
"title": "Mobiles",
"description": "",
"submenu": [
"0":
"info":
"megamenu_id": "67",
"language_id": "1",
"title": "Mobile Phones",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "68",
"language_id": "1",
"title": "Android Phones",
"description": ""
,
"infosub":
"megamenu_id": "69",
"language_id": "1",
"title": "Smart Phones",
"description": ""
,
"infosub":
"megamenu_id": "70",
"language_id": "1",
"title": "Windows Phones",
"description": ""
,
"infosub":
"megamenu_id": "72",
"language_id": "1",
"title": "Basic Phones",
"description": ""
,
"infosub":
"megamenu_id": "73",
"language_id": "1",
"title": "Latest Mobiles",
"description": ""
,
"infosub":
"megamenu_id": "74",
"language_id": "1",
"title": "Upcoming Mobiles",
"description": ""
]
,
"0":
"info":
"megamenu_id": "75",
"language_id": "1",
"title": "Brands",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "76",
"language_id": "1",
"title": "Samsung",
"description": ""
,
"infosub":
"megamenu_id": "77",
"language_id": "1",
"title": "Apple",
"description": ""
,
"infosub":
"megamenu_id": "78",
"language_id": "1",
"title": "Redmi",
"description": ""
,
"infosub":
"megamenu_id": "79",
"language_id": "1",
"title": "Nokia",
"description": ""
,
"infosub":
"megamenu_id": "81",
"language_id": "1",
"title": "Sony",
"description": ""
,
"infosub":
"megamenu_id": "82",
"language_id": "1",
"title": "Micromax",
"description": ""
,
"infosub":
"megamenu_id": "83",
"language_id": "1",
"title": "HTC",
"description": ""
,
"infosub":
"megamenu_id": "84",
"language_id": "1",
"title": "Motorola",
"description": ""
,
"infosub":
"megamenu_id": "85",
"language_id": "1",
"title": "Oppo",
"description": ""
,
"infosub":
"megamenu_id": "86",
"language_id": "1",
"title": "Vivo",
"description": ""
,
"infosub":
"megamenu_id": "87",
"language_id": "1",
"title": "Honor",
"description": ""
,
"infosub":
"megamenu_id": "88",
"language_id": "1",
"title": "Oneplus",
"description": ""
]
,
"0":
"info":
"megamenu_id": "94",
"language_id": "1",
"title": "Mobile Accessories",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "95",
"language_id": "1",
"title": "Bluetooth Headsets",
"description": ""
,
"infosub":
"megamenu_id": "96",
"language_id": "1",
"title": "Mobile Chargers",
"description": ""
,
"infosub":
"megamenu_id": "97",
"language_id": "1",
"title": "Mobiles Cases & Covers",
"description": ""
,
"infosub":
"megamenu_id": "98",
"language_id": "1",
"title": "Screen Protector",
"description": ""
,
"infosub":
"megamenu_id": "99",
"language_id": "1",
"title": "Mobile Batteries",
"description": ""
,
"infosub":
"megamenu_id": "100",
"language_id": "1",
"title": "Headphones & Headsets",
"description": ""
,
"infosub":
"megamenu_id": "101",
"language_id": "1",
"title": "Memory Cards",
"description": ""
,
"infosub":
"megamenu_id": "102",
"language_id": "1",
"title": "Power Banks",
"description": ""
,
"infosub":
"megamenu_id": "103",
"language_id": "1",
"title": "Stylus",
"description": ""
,
"infosub":
"megamenu_id": "104",
"language_id": "1",
"title": "Mobile Docks",
"description": ""
]
,
"0":
"info":
"megamenu_id": "107",
"language_id": "1",
"title": "Features",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "108",
"language_id": "1",
"title": "Dual Sim Phones",
"description": ""
,
"infosub":
"megamenu_id": "111",
"language_id": "1",
"title": "4GB Ram Mobiles",
"description": ""
,
"infosub":
"megamenu_id": "112",
"language_id": "1",
"title": "Rear Camera [13 MP]",
"description": ""
,
"infosub":
"megamenu_id": "113",
"language_id": "1",
"title": "Screen Size [4.5 - 5.2 Inch]",
"description": ""
]
]
,
"menu":
"megamenu_id": "168",
"language_id": "1",
"title": "Fashion",
"description": "",
"submenu": [
"0":
"info":
"megamenu_id": "169",
"language_id": "1",
"title": "Clothing",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "170",
"language_id": "1",
"title": "T-Shirts",
"description": ""
,
"infosub":
"megamenu_id": "171",
"language_id": "1",
"title": "Shirts",
"description": ""
]
,
"0":
"info":
"megamenu_id": "178",
"language_id": "1",
"title": "Womens Clothing",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "187",
"language_id": "1",
"title": "Night Wear",
"description": ""
,
"infosub":
"megamenu_id": "188",
"language_id": "1",
"title": "Tunics",
"description": ""
,
"infosub":
"megamenu_id": "189",
"language_id": "1",
"title": "Kurtis",
"description": ""
,
"infosub":
"megamenu_id": "190",
"language_id": "1",
"title": "Sarees",
"description": ""
,
"infosub":
"megamenu_id": "191",
"language_id": "1",
"title": "Lehengas",
"description": ""
,
"infosub":
"megamenu_id": "192",
"language_id": "1",
"title": "Swimming Suit",
"description": ""
]
,
"0":
"info":
"megamenu_id": "193",
"language_id": "1",
"title": "Men's Footwear",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "194",
"language_id": "1",
"title": "Shoes",
"description": ""
,
"infosub":
"megamenu_id": "195",
"language_id": "1",
"title": "Sneakers",
"description": ""
,
"infosub":
"megamenu_id": "196",
"language_id": "1",
"title": "Sandals & Floaters",
"description": ""
,
"infosub":
"megamenu_id": "197",
"language_id": "1",
"title": "Slippers & Flip Flops",
"description": ""
,
"infosub":
"megamenu_id": "198",
"language_id": "1",
"title": "Loafers",
"description": ""
,
"infosub":
"megamenu_id": "199",
"language_id": "1",
"title": "Boots",
"description": ""
,
"infosub":
"megamenu_id": "200",
"language_id": "1",
"title": "Shoe Care",
"description": ""
]
,
"0":
"info":
"megamenu_id": "201",
"language_id": "1",
"title": "Women's Jewellery",
"description": ""
,
"subsubmenu": [
"infosub":
"megamenu_id": "202",
"language_id": "1",
"title": "Earrings",
"description": ""
,
"infosub":
"megamenu_id": "203",
"language_id": "1",
"title": "Bangles",
"description": ""
,
"infosub":
"megamenu_id": "204",
"language_id": "1",
"title": "Bracelets",
"description": ""
,
"infosub":
"megamenu_id": "205",
"language_id": "1",
"title": "Rings",
"description": ""
]
]
,
"menu":
"megamenu_id": "220",
"language_id": "1",
"title": "Care",
"description": ""
,
"menu":
"megamenu_id": "221",
"language_id": "1",
"title": "Offers",
"description": ""
,
"menu":
"megamenu_id": "222",
"language_id": "1",
"title": "News",
"description": ""
]
【问题讨论】:
【参考方案1】:这可以使用 built_value 来完成。此link 中提供了详细文档。
您只需编写一些样板代码并运行此命令flutter packages pub run build_runner build
。
下面是一个类似 POJO 的示例类。
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'auth.g.dart';
abstract class Auth implements Built<Auth, AuthBuilder>
static Serializer<Auth> get serializer => _$authSerializer;
String get currentServerTime;
int get defaultOrganization;
String get tokenExpiryTimeInMs;
bool get rememberMe;
int get failedLoginAttempts;
int get userId;
String get status;
String get token;
Auth._();
factory Auth([updates(AuthBuilder b)]) = _$Auth;
下面是序列化器类:
library serializers;
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'auth/auth.dart';
part 'serializers.g.dart';
@SerializersFor(const [
Auth,
])
Serializers serializers = _$serializers;
Serializers standardSerializers =
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
以下是从 JSON 转换为 Object 的代码。
Auth auth = standardSerializers.deserializeWith(
Auth.serializer, json.decode(res.body)['user']);
希望这会有所帮助。
【讨论】:
我试过了,它没有解析数组的方法,适用于简单对象但无法解析数组,在我的情况下,我有 3 级数组 对于 built_collection 附带的可迭代对象,有一个名为BuiltList
的东西。但我不需要使用这个是的。也许你可以看看这个。【参考方案2】:
json_serializable 没有很好的文档记录,但它完全符合您的要求,比 built_value 更易于使用并且需要的样板文件更少,尤其是在涉及数组时。
import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';
part 'school.g.dart';
@JsonSerializable()
class School
final String name;
final int maxStudentCount;
final List<Student> students;
School(this.name, this.maxStudentCount, this.students);
factory School.fromJson(Map<String, dynamic> json) => _$SchoolFromJson(json);
Map<String, dynamic> toJson() => _$SchoolToJson(this);
@JsonSerializable()
class Student
final String name;
final DateTime birthDate;
Student(this.name, this.birthDate);
factory Student.fromJson(Map<String, dynamic> json) => _$StudentFromJson(json);
Map<String, dynamic> toJson() => _$StudentToJson(this);
test()
String jsonString = '''
"name":"Trump University",
"maxStudentCount":9999,
"students":[
"name":"Peter Parker",
"birthDate":"1999-01-01T00:00:00.000Z"
]
''';
final decodedJson = json.decode(jsonString);
final school = School.fromJson(decodedJson);
assert(school.students.length == 1);
它还支持枚举序列化。
要生成序列化代码,运行:
flutter packages pub run build_runner build
【讨论】:
【参考方案3】:所以,在对 json_serializable 库进行了一些尝试后,我想出了自己的解决方案,它根本不需要任何外部库,而且工作起来就像一个魅力。 这种方式我必须编写更少的样板代码,我认为这是一种干净的方式。
这是制作模型的方法
class SideMenuRes
final int status;
final String message;
final List<SideMenuDatum> sideMenuData;
SideMenuRes(this.status, this.message, this.sideMenuData);
factory SideMenuRes.fromJson(Map json)
return SideMenuRes(
status: json['status'],
message: json['message'],
sideMenuData: json['status'] == 200 ? (json['data'] as List).map((i) => new
SideMenuDatum.fromJson(i)).toList() : null,
);
class SideMenuDatum
final Menu menu;
SideMenuDatum(this.menu);
factory SideMenuDatum.fromJson(Map json)
return SideMenuDatum(
menu: Menu.fromJson(json['menu']),
);
class Menu
final String megamenu_id;
final String language_id;
final String title;
final String description;
final List<SubMenu> subMenu;
Menu(this.megamenu_id, this.language_id, this.title, this.description, this.subMenu);
factory Menu.fromJson(Map json)
return Menu(
megamenu_id: json['megamenu_id'],
language_id: json['language_id'],
title: json['title'],
description: json['description'],
subMenu: json['submenu'] != null ? (json['submenu'] as List).map((i) => new SubMenu.fromJson(i)).toList() : null
);
class SubMenu
final Zero zero;
final List<SubSubMenu> subSubMenu;
SubMenu(this.zero, this.subSubMenu);
factory SubMenu.fromJson(Map json)
return SubMenu(
zero: Zero.fromJson(json['0']),
subSubMenu: (json['subsubmenu'] as List).map((i) => new SubSubMenu.fromJson(i)).toList()
);
class Zero
final Info info;
Zero(this.info);
factory Zero.fromJson(Map json)
return Zero(
info: Info.fromJson(json['info']),
);
class SubSubMenu
final InfoSub infoSub;
SubSubMenu(this.infoSub);
factory SubSubMenu.fromJson(Map json)
return SubSubMenu(
infoSub: InfoSub.fromJson(json['infosub'])
);
class InfoSub
final String megamenu_id;
final String language_id;
final String title;
final String description;
InfoSub(this.megamenu_id, this.language_id, this.title, this.description);
factory InfoSub.fromJson(Map json)
return InfoSub(
megamenu_id: json['megamenu_id'],
language_id: json['language_id'],
title: json['title'],
description: json['description']
);
class Info
final String megamenu_id;
final String language_id;
final String title;
final String description;
Info(this.megamenu_id, this.language_id, this.title, this.description);
factory Info.fromJson(Map json)
return Info(
megamenu_id: json['megamenu_id'],
language_id: json['language_id'],
title: json['title'],
description: json['description']
);
这样称呼它
SubMenuRes subMenuRes = SubMenuRes.fromJson(response.data);
就是这样!
【讨论】:
基本上和 json_serializable 生成的东西是一样的。确定可行,尽管手动编写所有序列化代码肯定比每个类 4 行代码更多样板。当你有很多这样的课程时,你很有可能会犯错误。另一方面,代码生成器不会出错;)使用 json_serializable 对性能没有负面影响 这个答案只是使用手动序列化,这是一项艰苦的工作,如果你想避免大多数样板 json_serializable 是要走的路@boformers 答案是大中型项目的最佳解决方案,您还可以注意,如果您想学习如何手动处理 JSON,或者您想学习如何手动处理 JSON在一个小项目中使用 JSON 符合这个答案。 你用过的json_serializable
库的链接在哪里?你真的认为你的答案应该被标记吗?
稍微谷歌搜索不会有什么坏处。或者你也可以看看下面的答案;)【参考方案4】:
Android Studio 插件:https://plugins.jetbrains.com/plugin/12562-jsontodart-json-to-dart-
在线:https://app.quicktype.io/?l=dart
手动解析:https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
从 JSON 生成 Dart 类
将 JSON 转换为 Dart 类
从 JSON 或 JSON-Schema 生成 Dart 类。
【讨论】:
搜索这个已经有一段时间了。非常感谢!【参考方案5】:请查看this post 以获得最适合您目标的选择
【讨论】:
以上是关于在 Flutter 中将 JSON 转换为类似于 android 的 POJO(对象)的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Go 中将 JSON 对象数组转换为具有默认值的结构数组?
在scala中将spark决策树模型调试字符串转换为嵌套JSON