arrays - 在 Swift 4 中从 Url 获取 JSON 数据

标签 arrays json swift dictionary swift4

所以我试图从这个 JSON 格式的网站获取数据。 我可以很好地获取 MTL 和 OBJ 数据,但是相机、aabb 和纹理数据给我带来了一些麻烦。

{
camera: {
  position: {
    x: -11.3932,
    y: 110.683,
    z: -18.2957
  },
  direction: {
    x: -0.40558,
    y: 0.40558,
    z: -0.819152
  },
  fov: 70
  },
aabb: {
  min: {
    x: -10.4467,
    y: 104.404,
    z: -13.709
  },
  max: {
    x: -6.74608,
    y: 111.39,
    z: -11.488
  }
},
mtl: "320a81679c114665a3eff9945968204d",
obj: "5f042b840ceb4a0211797eaf8cf75c01",
textures: [
  "e5dbadfc8a0feeaa79fc4c520b82d1e7",
  "0e48182cd907c44b36e21d624bf36b2a",
  "8d05159c75d0f5f7c8068bc928bb1a12",
  "db0fd6c20324aff4f145a58292874c4c",
  "2e01aa831a2c4b096ccac6b3fdf30279",
  "01840dc6d1548d496aab95701efbf69f",
  "31c7608579599b8bc85fa477d2dc7f9f",
  "cb877313d4525ced290a0560e372d1dc"
  ]
}

这是我在 Swift 4 方面的知识。 我可能做错了结构部分,但我在网上找不到任何可以告诉我如何使纹理数据工作的东西,我知道它是一本字典,但我仍然找不到有关如何获取数据的任何资源。我尝试过的所有方法都给我一个错误,提示格式不正确。

struct Tures: Decodable {
    let camera: String
    let aabb: String
    let mtl: String
    let obj: String
    let textures: String


init(jsontable: [String: Any]) {
    camera = jsontable["camera"] as? String ?? ""
    aabb = jsontable["aabb"] as? String ?? ""
    mtl = jsontable["mtl"] as? String ?? ""
    obj = jsontable["obj"] as? String ?? ""
    textures = jsontable["textures"] as? String ?? ""
}
}
        used = https://t0.rbxcdn.com/5e126639e3049c01ccd73fecb9416eb8
        guard let url = URL(string: used) else { return }
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else { return }
            //let dataAsString = String(data: data, encoding: .utf8)
            //print(dataAsString ?? "")


            do {
                guard let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else { return }

                let table2 = Tures(jsontable: json)
                let camera = table2.camera
                let aabb = table2.aabb
                let mtl = table2.mtl
                let obj = table2.obj
                let textures = table2.textures
                print(camera, aabb, textures)
            }
            catch {
            }
        }.resume()

如有任何帮助,我们将不胜感激。

最佳答案

对于相机,您需要为位置和方向创建自定义结构 XYZ(您可以为 AABB 最小值和最大值使用相同的结构)和 fov 属性。 Textures 它是一个字符串数组,而不是单个字符串。对于 aabb,您还需要一个具有 min 和 max 的结构,并使用 XYZ 结构作为属性类型:

struct Tures: Codable, CustomStringConvertible {
    let camera: Camera
    let aabb: AABB
    let mtl: String
    let obj: String
    let textures: [String]
    var description: String {
        return "Camera: \(camera) - AABB: \(aabb) - MTL: \(mtl) - OBJ: \(obj) - Textures: \(textures)"
    }
}
struct Camera: Codable, CustomStringConvertible {
    let position: XYZ
    let direction: XYZ
    let fov: Int
    var description: String {
        return "Position: \(position) - Direction: \(direction) - Fov: \(fov)"
    }
}
struct AABB: Codable, CustomStringConvertible{
    let min: XYZ
    let max: XYZ
    var description: String {
        return "Min: \(min) - Max: \(max)"
    }
}
struct XYZ: Codable, CustomStringConvertible  {
    let x: Double
    let y: Double
    let z: Double
    var description: String {
        return "X: \(x) - Y: \(y) - Z: \(z)"
    }
}

用法:

let json = """
{ "camera": {
        "position": {
            "x": -11.3932,
            "y": 110.683,
            "z": -18.2957 },
        "direction": {
            "x": -0.40558,
            "y": 0.40558,
            "z": -0.819152 },
        "fov": 70 },
    "aabb": {
        "min": {
            "x": -10.4467,
            "y": 104.404,
            "z": -13.709 },
        "max": {
            "x": -6.74608,
            "y": 111.39,
            "z": -11.488 } },
    "mtl": "320a81679c114665a3eff9945968204d",
    "obj": "5f042b840ceb4a0211797eaf8cf75c01",
    "textures": [
        "e5dbadfc8a0feeaa79fc4c520b82d1e7",
        "0e48182cd907c44b36e21d624bf36b2a",
        "8d05159c75d0f5f7c8068bc928bb1a12",
        "db0fd6c20324aff4f145a58292874c4c",
        "2e01aa831a2c4b096ccac6b3fdf30279",
        "01840dc6d1548d496aab95701efbf69f",
        "31c7608579599b8bc85fa477d2dc7f9f",
        "cb877313d4525ced290a0560e372d1dc"]}
"""

do {
    let tures = try JSONDecoder().decode(Tures.self, from: Data(json.utf8))
    print(tures)
} catch {
    print(error)
}

这将打印:

Camera: Position: X: -11.3932 - Y: 110.683 - Z: -18.2957 - Direction: X: -0.40558 - Y: 0.40558 - Z: -0.819152 - Fov: 70 - AABB: Min: X: -10.4467 - Y: 104.404 - Z: -13.709 - Max: X: -6.74608 - Y: 111.39 - Z: -11.488 - MTL: 320a81679c114665a3eff9945968204d - OBJ: 5f042b840ceb4a0211797eaf8cf75c01 - Textures: ["e5dbadfc8a0feeaa79fc4c520b82d1e7", "0e48182cd907c44b36e21d624bf36b2a", "8d05159c75d0f5f7c8068bc928bb1a12", "db0fd6c20324aff4f145a58292874c4c", "2e01aa831a2c4b096ccac6b3fdf30279", "01840dc6d1548d496aab95701efbf69f", "31c7608579599b8bc85fa477d2dc7f9f", "cb877313d4525ced290a0560e372d1dc"]

关于arrays - 在 Swift 4 中从 Url 获取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47463071/

相关文章:

php - 如何从 MySQL 查询(多级数组)中获取变量?

c++ - 二维数组,IF条件检查

java - 可变大小数组的构造函数

json - Newtonsoft Json 模式

c# - 如何在强制 JSON 序列化中存在 "false" bool 值的同时省略过时的字段?

ios - Swift 中的远程通知没有声音

Python 元组数组

php - 从 PHP 查询中获取 Json

swift - 文本字段不起作用

ios - 如何在没有动画的情况下隐藏键盘(或加速此动画)