Flutter中如何将WordPress API返回的嵌套ArrayList转换为Model类(POJO)
wp:term Array in Flutter WordPress API Models Got it, let's tackle this nested array issue from the WordPress API in Flutter. That wp:term structure is a bit tricky since it's a 2D array (array of arrays of term objects), but it's totally manageable once you map it correctly to Dart types.
First, let's break down the structure: your JSON wp:term is [[{termObject}]] — so it's a list of lists of term objects. In Dart, this translates directly to List<List<Term>>, where Term is the model for the inner term data.
Step 1: Define the Inner Term Model
First, create a model class for the individual term objects (the innermost data):
class Term { final int id; final String link; final String name; final String slug; final String taxonomy; Term({ required this.id, required this.link, required this.name, required this.slug, required this.taxonomy, }); // Manual JSON deserialization factory Term.fromJson(Map<String, dynamic> json) { return Term( id: json['id'] as int, link: json['link'] as String, name: json['name'] as String, slug: json['slug'] as String, taxonomy: json['taxonomy'] as String, ); } // Optional: Convert back to JSON Map<String, dynamic> toJson() { return { 'id': id, 'link': link, 'name': name, 'slug': slug, 'taxonomy': taxonomy, }; } }
Step 2: Add the Nested List to Your Parent Model
Next, in your parent model (e.g., Post for a WordPress post), define the wpTerm field as List<List<Term>> and handle the nested deserialization.
Option 1: Manual Deserialization
If you prefer not to use code generation packages, handle the nested array directly in the parent model's fromJson factory:
class Post { // Add your other post fields here (e.g., id, title, content) final List<List<Term>> wpTerm; Post({ // Initialize other fields... required this.wpTerm, }); factory Post.fromJson(Map<String, dynamic> json) { // Extract the nested wp:term array final wpTermRaw = json['wp:term'] as List<dynamic>; // Convert the 2D array to List<List<Term>> final List<List<Term>> wpTermList = wpTermRaw.map((innerList) { return (innerList as List<dynamic>) .map((termJson) => Term.fromJson(termJson as Map<String, dynamic>)) .toList(); }).toList(); return Post( // Initialize other fields from json... wpTerm: wpTermList, ); } }
Option 2: Use json_serializable (Code Generation)
If you use the popular json_serializable package for serialization, it can handle the nested list automatically with a small tweak for the field name.
- First, add dependencies to your
pubspec.yaml:
dependencies: json_annotation: ^4.8.1 dev_dependencies: build_runner: ^2.4.4 json_serializable: ^6.7.0
- Update the
Termmodel for code generation:
import 'package:json_annotation/json_annotation.dart'; part 'term.g.dart'; @JsonSerializable() class Term { final int id; final String link; final String name; final String slug; final String taxonomy; Term({ required this.id, required this.link, required this.name, required this.slug, required this.taxonomy, }); factory Term.fromJson(Map<String, dynamic> json) => _$TermFromJson(json); Map<String, dynamic> toJson() => _$TermToJson(json); }
- Update the parent
Postmodel, using@JsonKeyto map thewp:termJSON key to your Dart field:
import 'package:json_annotation/json_annotation.dart'; import 'term.dart'; part 'post.g.dart'; @JsonSerializable() class Post { // Other post fields... @JsonKey(name: 'wp:term') final List<List<Term>> wpTerm; Post({ // Initialize other fields... required this.wpTerm, }); factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json); Map<String, dynamic> toJson() => _$PostToJson(json); }
- Run the code generation command in your terminal:
flutter pub run build_runner build
Key Notes
- The
List<List<Term>>type matches the JSON's 2D array structure perfectly. Each inner list typically represents a taxonomy type (e.g., one list for categories, another for tags). - If you ever need to access the terms, you can loop through the outer list to get each taxonomy group, then loop through the inner list for individual terms.
内容的提问来源于stack exchange,提问作者Sunil

