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

如何在swift xcode中为多个json表创建结构,json,swift,xcode,Json,Swift,Xcode,我正在编写一个需要读取JSON文件的IOS应用程序。 我知道最好的方法是为该json文件编写一个结构,并将json解析为该结构,以便能够自由使用 我有一个Json文件,保存在其中一个文件夹中 { "colors": [ { "color": "black", "category": "hue", "type": "primary", "code": { "rgba": [255,255,255,1], "

我正在编写一个需要读取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文件 并将我给出的结构分配给它,然后在代码中轻松地阅读它们


你能给我建议一种方法吗?

首先,你需要一个伞形结构来解码
颜色和

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
}
struct Person: Decodable {
    let firstName : String
    let isValid : Bool
    let friendsList : Friends
}

struct Friends: Decodable {
    let friendNames : [String]
    let age : Int
}
Person
相关的结构是

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

struct ColorCode: Decodable {
    let rgba : [UInt8]
    let hex : String
}
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:"/...."))
您可以使用

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

首先,您需要一个伞形结构来解码
颜色
人物

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
}
struct Person: Decodable {
    let firstName : String
    let isValid : Bool
    let friendsList : Friends
}

struct Friends: Decodable {
    let friendNames : [String]
    let age : Int
}
Person
相关的结构是

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

struct ColorCode: Decodable {
    let rgba : [UInt8]
    let hex : String
}
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:"/...."))
您可以使用

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

阅读苹果公司关于
Codable
protocol的文档阅读苹果公司关于
Codable
protocolit的文档对于数据行来说是错误的。-JSON文件位于一个名为“Resurces”的文件夹中,我添加了let data=try data(contentsOf:URL(fileURLWithPath:“/resources/colors.JSON”)),但它不编译
Resurces
where?您必须指定文件的实际完整路径。如果文件在bundle中,URL是
bundle.main.URL(forResource:“colors”,扩展名为:“json”)
。为我工作给了你最好的答案,这意味着我们创建了一个Root:Decodable,这是否意味着我不能在我的应用程序中对其他内容使用可解码或结构?您必须始终从顶级对象解码JSON,该对象由
结构表示(名称是任意的),并且所有结构必须匹配相应的JSON数据。谢谢,如果我想在函数中返回结果,函数将返回什么类型?对于数据行,它显示为错误。-JSON文件位于一个名为“Resurces”的文件夹中,我添加了let data=try data(contentsOf:URL(fileURLWithPath:“/resources/colors.JSON”)),但它不编译
Resurces
where?您必须指定文件的实际完整路径。如果文件在bundle中,URL是
bundle.main.URL(forResource:“colors”,扩展名为:“json”)
。为我工作给了你最好的答案,这意味着我们创建了一个Root:Decodable,这是否意味着我不能在我的应用程序中对其他内容使用可解码或结构?您必须始终从顶级对象解码JSON,该对象由
结构表示(名称是任意的),并且所有结构必须匹配相应的JSON数据。谢谢,如果我想在函数中返回结果,函数将返回什么类型?