Sample Code

Using JSON with Custom Types JSONをあつらえの型で使う

Encode and decode JSON data, regardless of its structure, using Swift’s JSON support. JSONデータをエンコードおよびデコードします、それの構造に関係なく、SwiftのJSONサポートを使います。
Download

Overview 概要

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データとあなたのアプリの持つモデル型の間の相違点を取り扱ってください。

Art

This sample defines a simple data type, GroceryProduct, and demonstrates how to construct instances of that type from several different JSON formats. このサンプルは、単純なデータ型、GroceryProductを定義します、そしてどのようにその型のインスタンスを幾つかの異なるJSON形式から組み立てるかを実演します。


struct GroceryProduct: Codable {
    var name: String
    var points: Int
    var description: String?
}

Read Data from Arrays 配列からデータを読み出す

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を扱う方法を見ます:


[
    {
        "name": "Banana",
        "points": 200,
        "description": "A banana grown in Ecuador."
    }
]

Change Key Names キー名を変更する

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_name" key in the JSON below to the name property on GroceryProduct: JSONキーから、あなたのあつらえの型に関するプロパティに関連づける(マップする)方法を学んでください、それらの名前に関係なく。例えば、このプレイグラウンドは、下のJSONの中の"product_name"キーをnameプロパティにGroceryProduct上でマップします:


{
    "product_name": "Banana",
    "product_cost": 200,
    "description": "A banana grown in Ecuador."
}

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キーの名前が異なるとしてもです。

Access Nested Data 入れ子にされたデータにアクセスする

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から抜き出して無用のデータと構造を省く方法を見ます:


[
    {
        "name": "Home Town Market",
        "aisles": [
            {
                "name": "Produce",
                "shelves": [
                    {
                        "name": "Discount Produce",
                        "product": {
                            "name": "Banana",
                            "points": 200,
                            "description": "A banana that's perfectly ripe."
                        }
                    }
                ]
            }
        ]
    }
]

Merge Data at Different Depths 異なる深さでのデータを1つにする

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 GroceryProduct instance from JSON that looks like this: JSON構造体の異なる深さからデータを結合または分離を、EncodableDecodableからのプロトコル要件のあつらえの実装を書くことで行ってください。このプレイグラウンドは、GroceryProductインスタンスをこのように見えるJSONから組み立てる方法を示します:


{
    "Banana": {
        "points": 200,
        "description": "A banana grown in Ecuador."
    }
}

See Also 参照

JSON