Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从Swift 4中的Url获取JSON数据_Arrays_Json_Swift_Dictionary_Swift4 - Fatal编程技术网

Arrays 从Swift 4中的Url获取JSON数据

Arrays 从Swift 4中的Url获取JSON数据,arrays,json,swift,dictionary,swift4,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: {

所以我试图从一个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"
  ]
}
以下是我对事物的四个方面的看法。 我可能把结构部分弄错了,但我在网上找不到任何东西可以告诉我如何使用纹理数据,我知道这是一本字典,但我仍然找不到任何关于如何获取数据的资源。我尝试过的每件事都会给我一个错误,说格式不正确

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 min和max使用相同的结构)和fov属性。纹理它是一个字符串数组,而不是单个字符串。对于aabb,您还需要一个具有最小值和最大值的结构,并使用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"]}
"""

这将打印:

摄像机:位置:X:-11.3932-Y:110.683-Z:-18.2957-方向: 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-纹理: [“e5dbadfc8a0feeaa79fc4c520b82d1e7”, “0e48182cd907c44b36e21d624bf36b2a”, “8d05159c75d0f5f7c8068bc928bb1a12”, “DB0FD6C20324AFF4F145A58292874C”, “2E01A831A2C4B096CCAC6B3FDF30279”, “01840DC6D1548D496AB95701EFBF69F”, “31c7608579599b8bc85fa477d2dc7f9f”, “cb877313d4525ced290a0560e372d1dc”]


对于相机,需要为位置和方向创建自定义结构XYZ(可以为AABB min和max使用相同的结构)和fov属性。纹理它是一个字符串数组,而不是单个字符串。对于aabb,您还需要一个具有最小值和最大值的结构,并使用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"]}
"""

这将打印:

摄像机:位置:X:-11.3932-Y:110.683-Z:-18.2957-方向: 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-纹理: [“e5dbadfc8a0feeaa79fc4c520b82d1e7”, “0e48182cd907c44b36e21d624bf36b2a”, “8d05159c75d0f5f7c8068bc928bb1a12”, “DB0FD6C20324AFF4F145A58292874C”, “2E01A831A2C4B096CCAC6B3FDF30279”, “01840DC6D1548D496AB95701EFBF69F”, “31c7608579599b8bc85fa477d2dc7f9f”, “cb877313d4525ced290a0560e372d1dc”]


谢谢你,伙计!我知道我需要做什么,现在我知道未来该做什么。我在尝试使用该网站作为json使用时遇到了一些问题,但我发现我只需刮取网页并使用“let son=(“+WebData+”)”就可以很好地读取信息。再次感谢您的帮助!:)谢谢你,伙计!我知道我需要做什么,现在我知道未来该做什么。我在尝试使用该网站作为json使用时遇到了一些问题,但我发现我只需刮取网页并使用“let son=(“+WebData+”)”就可以很好地读取信息。再次感谢您的帮助!:)