You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter Dart中解析指定JSON数据的每个值?

在Dart中解析该JSON的方法

首先注意:你提供的JSON是非标准格式(键未加双引号),第一步必须修正为标准JSON,否则Dart的JSON解析库会报错,修正后的JSON如下:

{
  "0": {"id": 1, "user_id": 1, "title": "Group A", "open": true},
  "1": {"id": 2, "user_id": 1, "title": "yiuyi", "open": false},
  "pagination": {"total": 2, "per_page": 10, "current_page": 1, "last_page": 1, "next_page_url": null, "prev_page_url": null}
}

方法一:手动解析(无需第三方库)

直接用Dart内置的dart:convert库完成解析,步骤如下:

  1. 导入核心库:
import 'dart:convert';
  1. 将JSON字符串转为Map:
// 假设jsonStr是修正后的标准JSON字符串
final jsonMap = jsonDecode(jsonStr) as Map<String, dynamic>;
  1. 提取分组数据:
    遍历Map中键为数字的条目,将每个分组转为可直接访问的结构:
List<Map<String, dynamic>> groups = [];
jsonMap.forEach((key, value) {
  // 判断键是否为有效数字字符串
  if (int.tryParse(key) != null) {
    groups.add(value as Map<String, dynamic>);
  }
});

// 访问分组字段示例
print(groups[0]['title']); // 输出:Group A
print(groups[1]['open']); // 输出:false
  1. 提取分页数据:
    直接从Map中取出pagination子节点,提取各字段:
final pagination = jsonMap['pagination'] as Map<String, dynamic>;
int total = pagination['total'] as int;
int perPage = pagination['per_page'] as int;
int currentPage = pagination['current_page'] as int;

// 输出示例
print('总条数:$total,当前页:$currentPage');

方法二:模型类解析(适合复杂项目)

如果需要更规范的代码结构,可定义对应模型类,让解析逻辑更清晰:

步骤1:定义模型类

// 分组模型
class Group {
  final int id;
  final int userId;
  final String title;
  final bool open;

  Group({
    required this.id,
    required this.userId,
    required this.title,
    required this.open,
  });

  // 从Map转Group对象
  factory Group.fromJson(Map<String, dynamic> json) {
    return Group(
      id: json['id'] as int,
      userId: json['user_id'] as int,
      title: json['title'] as String,
      open: json['open'] as bool,
    );
  }
}

// 分页模型
class Pagination {
  final int total;
  final int perPage;
  final int currentPage;
  final int lastPage;
  final String? nextPageUrl;
  final String? prevPageUrl;

  Pagination({
    required this.total,
    required this.perPage,
    required this.currentPage,
    required this.lastPage,
    this.nextPageUrl,
    this.prevPageUrl,
  });

  // 从Map转Pagination对象
  factory Pagination.fromJson(Map<String, dynamic> json) {
    return Pagination(
      total: json['total'] as int,
      perPage: json['per_page'] as int,
      currentPage: json['current_page'] as int,
      lastPage: json['last_page'] as int,
      nextPageUrl: json['next_page_url'] as String?,
      prevPageUrl: json['prev_page_url'] as String?,
    );
  }
}

步骤2:解析JSON到模型对象

final jsonMap = jsonDecode(jsonStr) as Map<String, dynamic>;

// 解析分组列表
List<Group> groups = [];
jsonMap.forEach((key, value) {
  if (int.tryParse(key) != null) {
    groups.add(Group.fromJson(value as Map<String, dynamic>));
  }
});

// 访问分组数据示例
print(groups[0].title); // Group A
print(groups[1].open); // false

// 解析分页数据
final pagination = Pagination.fromJson(jsonMap['pagination'] as Map<String, dynamic>);
print('总页数:${pagination.lastPage}'); // 1

如果项目频繁处理JSON,也可以用json_serializable代码生成库自动生成序列化逻辑,避免手动编写fromJson方法,上述手动实现已足以应对当前场景。

内容的提问来源于stack exchange,提问作者Kamlesh Kumar Kushwaha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 17:37:37