所以我试图从这个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()
对此有任何帮助将不胜感激。
答案 0 :(得分:0)
对于相机,您需要为位置和方向创建自定义结构XYZ(您可以为AABB min和max使用相同的结构)和fov的属性。纹理它是一个字符串数组,而不是一个字符串。对于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:Min:X: -10.4467 - Y:104.404 - Z:-13.709 - 最大值:X:-6.74608 - Y:111.39 - Z:-11.488 - MTL:320a81679c114665a3eff9945968204d - OBJ:5f042b840ceb4a0211797eaf8cf75c01 - 纹理: [" e5dbadfc8a0feeaa79fc4c520b82d1e7&#34 ;, " 0e48182cd907c44b36e21d624bf36b2a&#34 ;, " 8d05159c75d0f5f7c8068bc928bb1a12&#34 ;, " db0fd6c20324aff4f145a58292874c4c&#34 ;, " 2e01aa831a2c4b096ccac6b3fdf30279&#34 ;, " 01840dc6d1548d496aab95701efbf69f&#34 ;, " 31c7608579599b8bc85fa477d2dc7f9f&#34 ;, " cb877313d4525ced290a0560e372d1dc"]