【发布时间】:2015-11-19 07:48:15
【问题描述】:
我正在尝试使用下一种格式解析 JSON 数据:
["1", "string", "", "0.16"]
这些“奇怪”的 json 应该以下一个方式映射到我的对象:
myObject.id = json[0] //"1"
myObject.name = json[1] //"string"
myObject.surname = json[2] // ""
myObject.length = json[3] // "0.16"
我正在使用 Argo 进行解析,有我的代码示例
public struct SomeObject {
public var id: String
public var name: String
public var surname: String
public var length: Float
}
extension SomeObject: Decodable {
static func create(id: String)(name: String)(surname: String)(length: String) -> SomeObject {
return SomeObject(id: id, name: name, surname: surname, length: length)
}
public static func decode(json: JSON) -> Decoded<SomeObject> {
return SomeObject.create <- actually don't know what to put here, i tried json[0], and decode(json[0]) and casting but still no luck
}
解析这种 JSON 数据的正确方法是什么?
【问题讨论】: