【问题标题】:Getting JSON Data From Url In Swift 4从 Swift 4 中的 Url 获取 JSON 数据
【发布时间】:2017-11-23 20:47:36
【问题描述】:

所以我试图从这个 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()

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: arrays json swift dictionary swift4


    【解决方案1】:

    对于相机,您需要为位置和方向创建一个自定义结构 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)
    }
    

    这将打印:

    相机:位置:X:-11.3932 - Y:110.683 - Z:-18.2957 - 方向: X:-0.40558 - Y:0.40558 - Z:-0.819152 - Fov:70 - AABB:最小值:X: -10.4467 - Y:104.404 - Z:-13.709 - 最大值:X:-6.74608 - Y:111.39 - Z:-11.488 - MTL:320a81679c114665a3eff9945968204d - OBJ:5f042b840ceb4a0211797eaf8 [“e5dbadfc8a0feeaa79fc4c520b82d1e7”, "0e48182cd907c44b36e21d624bf36b2a", “8d05159c75d0f5f7c8068bc928bb1a12”, "db0fd6c20324aff4f145a58292874c4c", “2e01aa831a2c4b096ccac6b3fdf30279”, "01840dc6d1548d496aab95701efbf69f", “31c7608579599b8bc85fa477d2dc7f9f”, "cb877313d4525ced290a0560e372d1dc"]

    【讨论】:

    • 谢谢你!我知道我需要做什么,现在我知道为未来做什么。我在尝试将网站用作 json 用法时遇到了一些问题,但我发现我只是抓取网页并使用,“let son = ("" + WebData + "")" 然后它能够​​很好地读取信息.再次感谢您的帮助! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多