• 在 Dart 里使用 JSON
    • 需要用到的库
    • 相关的 Flutter 资源
    • 相关的 Web 应用资源
    • 相关的 VM 资源

    在 Dart 里使用 JSON

    许多移动或网页应用都使用 JSON 来处理类似与服务器交换数据的任务。本文将讨论如果使用 Dart 对 JSON 数据进行序列化反序列化:即 Dart 对象与 JSON 数据之间的相互转换。

    需要用到的库

    下述的库和包可以用作于所有的 Dart 平台:

    • dart:convert包含 JSON 数据和 UTF-8(JSON 数据需要的字符编码)的转换器。

    • package:json_serializable一个易于使用的代码生成包。当你添加一些元注解以及使用该包提供的构建器时,Dart 编译器可以为你生成序列化和反序列化的代码。

    • package:built_value一个强大的,可以作为 json_serializable 的替代品。

    相关的 Flutter 资源

    • JSON and serialization
    • Shows how Flutter apps can serialize and deserialize bothwith dart:convert and with json_serializable.

    JSON 和序列化:向你展示 Flutter 应用是如何使用 dart:convert 和 json_serializable 进行序列化和反序列化的。

    相关的 Web 应用资源

    • AngularDart Tutorial, part 6: HTTP
    • Illustrates how a Dart web app can interact with aRESTful backend using JSON data.

    AngularDart 教程的第六部分:HTTP:向你说明 Dart Web 应用如何使用 JSON 数据与 RESTful 后端进行交互。

    • Using HTTP resources with HttpRequest
    • Demonstrates how to use HttpRequest to exchange data with a web server.Part of the dart:html library tour.

    通过 HttpRequest 使用 HTTP 资源:向你演示如何使用 HttpRequest 与服务器交换数据。该资源是 dart:html 库概览 的一部分。

    相关的 VM 资源

    • Write HTTP Clients & Servers
    • Walks through how to implement command-line clients and serversthat exchange JSON data.

    开发 HTTP 客户端和服务端:手把手教你如何实现一个命令行式的客户端和服务端并使用 JSON 交换数据。