ios - JSON 到自定义对象数组 (Swift)

标签 ios arrays json swift swift4

我有一个 JSON,我需要它来将它转换成对象数组。 这是我的 JSON(它的简短版本)

[
{
    "categoryID": 5,
    "categoryDescription": "Trips",
    "groupID": 43,
    "groupDescription": "USA",
    "groupImage": "e613c87a-4dab-4929-90cf-2b584fdf0399.jpg",
    "subgroupDescription": "2 days",
    "subgroupPrice": "200"
},
{
   "categoryID": 5,
    "categoryDescription": "Trips",
    "groupID": 43,
    "groupDescription": "USA",
    "groupImage": "e613c87a-4dab-4929-90cf-2b584fdf0399.jpg",
    "subgroupDescription": "5 days",
    "subgroupPrice": "500"
},
{
    "categoryID": 5,
    "categoryDescription": "Trips",
    "groupID": 33,
    "groupDescription": "Mexico",
    "groupImage": "e613c87a-4dab-4929-90cf-2b584fdf0399.jpg",
    "subgroupDescription": "3 days",
    "subgroupPrice": "400"
},
{
    "categoryID": 5,
    "categoryDescription": "Trips",
    "groupID": 33,
    "groupDescription": "Mexico",
    "groupImage": "e613c87a-4dab-4929-90cf-2b584fdf0399.jpg",
    "subgroupDescription": "1 days",
    "subgroupPrice": "150"
},
{
    "categoryID": 3,
    "categoryDescription": "Hotels",
    "groupID": 22,
    "groupDescription": "My Hotel",
    "groupImage": "e613c87a-4dab-4929-90cf-2b584fdf0399.jpg",
    "subgroupDescription": "1 night",
    "subgroupPrice": "50"
},
{
    "categoryID": 3,
    "categoryDescription": "Hotels",
    "groupID": 10,
    "groupDescription": "Your Hotel",
    "groupImage": "e613c87a-4dab-4929-90cf-2b584fdf0399.jpg",
    "subgroupDescription": "2 nights",
    "subgroupPrice": "150"
}]

我需要制作这样的自定义对象:(如何在 Swift 4 中创建这样的对象?)

enter image description here

在 JSON 中,我们有 categoryIDgroupID 我需要根据它们过滤数组,例如我只需要一个 categoryID 为 5 的对象。我需要只有一个组 ID 为 43 的对象。但我需要所有子组。 这是我的对象的结构:(我不知道我是否在正确的道路上)

struct Categories {
var categoryID: NSNumber
var categoryDescription: String
var groups : [Groups]}

struct Groups {
var geoupID: NSNumber
var geoupDescription: String
var groupImage: String
var subGroups : [Subgroups] }

struct Subgroups {
var subgroupPrice: NSNumber
var subgroupDescription: String }

如何以最佳方式过滤它?

最佳答案

至于如何制作结构,只需使用 Decodable 并根据您在 JSON 中期望的内容命名字段,或者使用 CodingKeys 指定名称。例如:

struct MyStruct: Decodable {
    let categoryID: Int
    let categoryDescription: String
    let groupID: Int
    // …
}

然后您可以使用 JSONDecoder 将结果解码为 [MyStruct]

现在,真正的问题似乎是您希望内部结构与您收到的 JSON 结构不同,包括像“每个 categoryID 中只有一个”这样的约束。可能最直接的方法是遍历解码结果并将内容复制到不同的结构中。

代替数组(例如,var groups: [Groups])你可以有一个字典以 groupID 作为键来强制每个 id 只有一个,对于示例:

guard let results = try? jsonDecoder.decode([MyStruct].self, from: json) else { return }
var categories = [Int: Category]()
for result in results {
    // fetch existing category or make a new one
    var category = categories[result.categoryID, default: Category(id: result.categoryID, description: result.categoryDescription)]

    // fetch existing group in category or make a new one
    var group = category.groups[result.groupID, default: Group(id: result.groupID, description: result.groupDescription, image: result.groupImage)]

    // append subgroup (always new since there is no id)
    let subgroup = Subgroup(description: result.subgroupDescription, price: result.subgroupPrice)
    group.subgroups.append(subgroup)

    // "save"
    category.groups[result.groupID] = group
    categories[result.categoryID] = category
}

关于ios - JSON 到自定义对象数组 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52408386/

相关文章:

json - 是否可以使用 serde_json 反序列化看起来像 JSON(但不是)的数据?

iphone - UITableView 单元格返回 nil 属性值

ios - 玩家滑动以删除比赛

ios - 我如何确定 App Transport Security 阻止了哪个 URL?

c++ - C++中指向指针动态数组的指针

jquery - XMLHttpRequest 无法使用 jQuery 加载 URL

ios - 将 React Native RCTRootView 关闭回 Native ViewController

c - 如何在while循环中使用结构数组

arrays - 使用 NSSet 构造可用的 Swift 数组时遇到问题

json - 在序列化时将字符串属性值转换为 base64 对于 ASP.NET Core 3.0+