class JSONEncoder
class JSONDecoder
class JSONSerialization
Availability 有効性
Technology
JSON data you send or receive from other apps, services, and files can come in many different shapes and structures. Use the techniques described in this sample to handle the differences between external JSON data and your app’s model types. 他のアプリ、サービス、そしてファイルからあなたが受け取るまたは送るJSONデータは、多くの異なる形状や構造でやってきます。このサンプルで記述されるテクニックを使って、外部JSONデータとあなたのアプリの持つモデル型の間の相違点を取り扱ってください。
This sample defines a simple data type, Grocery
, and demonstrates how to construct instances of that type from several different JSON formats.
このサンプルは、単純なデータ型、Grocery
を定義します、そしてどのようにその型のインスタンスを幾つかの異なるJSON形式から組み立てるかを実演します。
Use Swift’s expressive type system to avoid manually looping over collections of identically structured objects. This playground uses array types as values to see how to work with JSON that’s structured like this: Swiftの表現に富む型システムを使って、同一に構造化されたオブジェクトそれらのコレクションすべてにわたって手動で見ていくことを回避してください。このプレイグラウンドは、配列型を値として使って、このように組み立てられたJSONを扱う方法を見ます:
Learn how to map data from JSON keys into properties on your custom types, regardless of their names. For example, this playground shows how to map the "product
key in the JSON below to the name
property on Grocery
:
JSONキーから、あなたのあつらえの型に関するプロパティに関連づける(マップする)方法を学んでください、それらの名前に関係なく。例えば、このプレイグラウンドは、下のJSONの中の"product
キーをname
プロパティにGrocery
上でマップします:
Custom mappings let you to apply the Swift API Design Guidelines to the names of properties in your Swift model, even if the names of the JSON keys are different. あつらえのマッピングは、あなたにSwift API Design GuidelinesをあなたのSwiftモデルの中のプロパティの名前に適用させます、たとえJSONキーの名前が異なるとしてもです。
Learn how to ignore structure and data in JSON that you don’t need in your code. This playground uses an intermediate type to see how to extract grocery products from JSON that looks like this to skip over unwanted data and structure: どのようにあなたのコードで必要としないJSONの構造とデータを無視するかを学んでください。このプレイグラウンドは、ある仲介型を使って雑貨店商品をこのように見えるJSONから抜き出して無用のデータと構造を省く方法を見ます:
Combine or separate data from different depths of a JSON structure by writing custom implementations of protocol requirements from Encodable
and Decodable
. This playground shows how to construct a Grocery
instance from JSON that looks like this:
JSON構造体の異なる深さからデータを結合または分離を、Encodable
とDecodable
からのプロトコル要件のあつらえの実装を書くことで行ってください。このプレイグラウンドは、Grocery
インスタンスをこのように見えるJSONから組み立てる方法を示します:
class JSONEncoder
class JSONDecoder
class JSONSerialization