Jas*_*ngh 7 parsing android json pojo flutter
我只是想找到一种方法将 json 响应(来自 REST API)转换为 POJO(在 android 中使用),以便我可以将接收到的数据使用到我的应用程序中,因为使用 Map 将不足以作为我的数据m 接收有点复杂,包含 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": ""
}
},
Ami*_*ati 13
Android Studio 插件:https : //plugins.jetbrains.com/plugin/12562-jsontodart-json-to-dart-
在线:https : //javiercbk.github.io/json_to_dart/
手动解析:https : //medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
从 JSON 生成 Dart 类
将 JSON 转换为 Dart 类
从 JSON 或 JSON-Schema 生成 Dart 类。
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);
}
Run Code Online (Sandbox Code Playgroud)
它还支持枚举序列化。
要生成序列化代码,请运行:
flutter packages pub run build_runner build
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8309 次 |
最近记录: |