将JSON转换为属性选项数组

时间:2018-01-16 11:16:20

标签: json swift swift4

我有一个JSON文件:

{
  "sceneSettings":[
    {
      "showsStatistics":false,
      "autoenablesDefaultLighting":false,
      "antialiasingMode":"multisampling4X",
      "debugOptions":[
        {
          "showPhysicsShapes":false,
          "showBoundingBoxes":false,
          "showLightInfluences":false,
          "showLightExtents":false,
          "showPhysicsFields":false,
          "showWireframe":false,
          "renderAsWireframe":false,
          "showSkeletons":false,
          "showCreases":false,
          "showConstraints":false,
          "showCameras":false
        }
      ]
    }
  ]
}

我想获取debugOptions并将它们作为SCNDebugOptions数组返回,以便在ARkit中使用。

我目前有一个结构DebugOption

struct DebugOption: Decodable, Loopable {
    let showPhysicsShapes: Bool = false
    let showBoundingBoxes: Bool  = false
    let showLightInfluences: Bool = false
    let showLightExtents: Bool = false
    let showPhysicsFields: Bool = false
    let showWireframe: Bool = false
    let renderAsWireframe: Bool = false
    let showSkeletons: Bool = false
    let showCreases: Bool = false
    let showConstraints: Bool = false
    let showCameras: Bool = false
}

使用以下方法解码json文件:

class SceneSettingsFactory {

    func parseJSON() -> SceneSettings? {
        if let url = Bundle.main.url(forResource: "ModelsData", withExtension: "json") {
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                let jsonData = try decoder.decode(ResponseData.self, from: data)

                return jsonData.sceneSettings
            } catch {
                print("error:\(error)")
            }
        }
        return nil
    }
}

struct ResponseData: Decodable {
    var model: [Model]
    var sceneSettings: SceneSettings
}

struct SceneSettings: Decodable {
    let showsStatistics: Bool = false
    let autoenablesDefaultLighting: Bool = false
    let antialiasingMode: AntialiasingOption = .none
    let debugOptions: DebugOption
}

如果Bool值的值为真,我不确定如何有效地将Bool值转换为相应SCNDebugOptions的数组。

我首先将以下内容添加到我的DebugOption结构中,但它并不是最好的方式。

func getOptionArray() -> [SCNDebugOptions] {
    var optionsArray = [SCNDebugOptions]()
    let allPropertyValues = self.allProperties
    allPropertyValues.forEach { property in
        guard let valueIsTrue = property.value as? Bool else { return }
        let propertyAsOption = keyStringAsOption(property.key)
        if valueIsTrue {
            optionsArray.append(propertyAsOption)
        }
    }
}

private func keyStringAsOption(_ propertyString: String) -> SCNDebugOptions {
    switch propertyString {
        case ""
    }
}

任何见解或帮助都会很棒,因为这会让人感到很啰嗦。

1 个答案:

答案 0 :(得分:0)

struct ResponseData: Decodable {
    var model: [Model]
    var sceneSettings: SceneSettings
}

看起来错了。您的sceneSettings是数组,但您的类型为SceneSettings。我建议你创建这样的东西:

struct ResponseData: Decodable {
    var model: [Model]
    var sceneSettings: [SceneSetting]
}

此外,您应该将SceneSettings结构更新为SceneSetting