json - 如何在 swift xcode 中为多个 json 表创建结构

标签 json swift xcode

我正在编写一个需要读取 JSON 文件的 IOS 应用程序。 我知道最好的方法是为该 json 文件编写一个结构并将 json 解析为该结构以便能够自由使用。

我有一个 Json 文件本地保存在其中一个文件夹中

{
  "colors": [
    {
      "color": "black",
      "category": "hue",
      "type": "primary",
      "code": {
        "rgba": [255,255,255,1],
        "hex": "#000"
      }
    },
    {
      "color": "white",
      "category": "value",
      "code": {
        "rgba": [0,0,0,1],
        "hex": "#FFF"
      }
    },
    {
      "color": "red",
      "category": "hue",
      "type": "primary",
      "code": {
        "rgba": [255,0,0,1],
        "hex": "#FF0"
      }
    },
    {
      "color": "blue",
      "category": "hue",
      "type": "primary",
      "code": {
        "rgba": [0,0,255,1],
        "hex": "#00F"
      }
    },
    {
      "color": "yellow",
      "category": "hue",
      "type": "primary",
      "code": {
        "rgba": [255,255,0,1],
        "hex": "#FF0"
      }
    },
    {
      "color": "green",
      "category": "hue",
      "type": "secondary",
      "code": {
        "rgba": [0,255,0,1],
        "hex": "#0F0"
      }
    },
  ],

  "people": [
    {
      "first_name": "john",
      "is_valid": true,
      "friends_list": {
        "friend_names": ["black", "hub", "good"],
        "age": 13
      }
    },
    {
      "first_name": "michal",
      "is_valid": true,
      "friends_list": {
        "friend_names": ["jessy", "lyn", "good"],
        "age": 19
      }
    },
    {
      "first_name": "sandy",
      "is_valid": false,
      "friends_list": {
        "friend_names": ["brown", "hub", "good"],
        "age": 15
      }
    },
  ]
}

我为两个表中的每一个创建了一个结构:

import Foundation

struct Color {
    var color: String
    var category: String
    var type: String
    var code: [JsonCodeStruct]
}

struct Poeople {
    var firsName: String
    var is_valid: Bool
    var friendsNames: [JsonFriendNames]

}

struct JsonFriendNames {
    var friendNames: [String]
    var age: String
}

struct JsonCodeStruct {
    var rgba: [Double]
    var hex: String

}

我想打开本地的json文件 并将我提供的结构分配给它,然后在代码中轻松阅读它们。

你能建议我如何做到这一点吗?

最佳答案

首先,您需要一个伞形结构来解码colorspeople

struct Root: Decodable {
    let colors: [Color]
    let people : [Person]
}

您的结构中的类型部分错误。 Color 相关的结构是

struct Color: Decodable {
    let color: String
    let category: String
    let type: String?
    let code : ColorCode
}

struct ColorCode: Decodable {
    let rgba : [UInt8]
    let hex : String
}

Person相关的结构是

struct Person: Decodable {
    let firstName : String
    let isValid : Bool
    let friendsList : Friends
}

struct Friends: Decodable {
    let friendNames : [String]
    let age : Int
}

假设您使用

读取文件
let data = try Data(contentsOf: URL(fileURLWithPath:"/...."))

您可以将 JSON 解码为给定的结构

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
    let result = try decoder.decode(Root.self, from: data)
    print(result)
} catch { print(error) }

关于json - 如何在 swift xcode 中为多个 json 表创建结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52947241/

相关文章:

javascript - 如何在引号中插入JS代码?

json - 在Kotlin中使用GSON解析JSON

ios - ImageView : iOS, Swift 中显示一些随机图像而不是来自 URL 的实际图像

swift - Xcode 卡在运行 Playground 上

php - 数据没有从ajax发送到php

android - 使用Fuel向 body 发送GET请求

ios - Swift 如何在不中断 reloadData 的情况下显示视频

ios - Swift Realm 迁移创建从旧类型到新类型的引用

ios - 有 xcode 7 - 需要 6.4 和命令行工具

ios - 初始化 SKView 时出现奇怪的错误