我正在尝试JSON使用Alamofireand将一系列字典加载到一个对象中SwiftyJSON,但是当下载数据时,我收到错误:
keyNotFound(CodingKeys(stringValue: "inProrgresss", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"inProrgresss\", intValue: nil) (\ "inProgresss\").",underlyingError: nil))。
本地化描述只是说数据丢失。我的 JSON 在这里,我的代码是这样的:
struct CompData: Codable {
let inProrgresss: [[String: String]]
let past: [[String: String]]
let upcoming: [[String: String]]
}
func getData(url: URLConvertible) {
SVProgressHUD.show()
Alamofire.request(url).responseJSON { (dataResponse) in
if dataResponse.result.value != nil {
SVProgressHUD.dismiss()
let JSONVar = JSON(dataResponse.result.value)
let data = Data(dataResponse.data!)
print(data)
let decoder = JSONDecoder()
do {
let newData …Run Code Online (Sandbox Code Playgroud) 对于给定的 JSON,如下所示:
{
"store": {
"animals": [
{
"type": "dog"
},
{
"type": "cat"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我可以用 enum 解析它,type如下所示:
final class AnimalStore: Decodable {
let store: Store
}
extension AnimalStore {
struct Store: Decodable {
let animals: [Animal]
}
}
extension AnimalStore.Store {
struct Animal: Decodable {
let type: AnimalType?
}
}
extension AnimalStore.Store.Animal {
enum AnimalType: String, Decodable {
case dog = "dog"
case cat = "cat"
//case unknown = how such a case could …Run Code Online (Sandbox Code Playgroud) 我有一个符合可解码的结构。它有 50 个 String 属性和只有一个 Bool。该 bool 来自服务器,如字符串“false”/“true”或有时像整数 0/1,因此无法从框中解码。我怎样才能让它解码但不写大量的所有 50 个字符串属性的手动解码?也许以某种方式覆盖 Bool 的 decodeIfPresent,但我无法让它工作。我如何才能避免通过手动解码所有内容和所有内容并仅处理那个 Bool 来创建 init?如果可能,没有计算属性。
struct Response: Decodable {
var s1: String
var s2: String
var s3: String
//...........
var s50: String
var b1: Bool
var b2: Bool
}
Run Code Online (Sandbox Code Playgroud)
这是 json 示例:
{
"s1":"string"
"s2":"string"
"s3":"string"
//..........
"s50":"string"
"b1":"true"
"b2":"0"
}
Run Code Online (Sandbox Code Playgroud)
试过这个但不起作用(((
extension KeyedDecodingContainer { //Doesn't work, no execution
func decodeIfPresent(_ type: Bool.Type, forKey key: K) throws -> Bool {
return try! self.decodeIfPresent(Bool.self, forKey: key)
}
func decodeIfPresent(_ …Run Code Online (Sandbox Code Playgroud) 我有一个 json 数组,其中包含一个具有不同属性的项目列表。
{
"items": [
{
"id": "1",
"name": "name",
"propertyOfA": "1243",
"productype": "a"
},
{
"id": "2",
"name": "name",
"propertyOfA": "12",
"productype": "a"
},
{
"id": "3",
"name": "name",
"propertyOfA": "1243",
"productype": "a"
},
{
"id": "1",
"name": "name",
"propertyOfB": "1243",
"productype": "b"
},
{
"id": "1",
"name": "name",
"propertyOfC": "1243",
"propertyOfC2": "1243",
"productype": "c"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我通常做的是将其解析为:
struct RequestData: Codable {
let items: [Item]
}
enum ProductType: String, Codable {
case a = "A"
case b …Run Code Online (Sandbox Code Playgroud) 我正在使用一个与此非常相似的 API:
{
"email": "hello@example.com",
"settings": [
{
"setting_A": {
"value": -65,
}
},
{
"setting_B": {
"value": {
"b1": {
"val": 12,
"unit": "kg"
},
"b2": {
"val": 10,
"unit": "g"
}
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我一直在解码这个 JSON 结果 esp。settings. 有人可以帮我怎么做吗?
这是我到目前为止:
struct User: Decodable {
let email: String
let settings: [Setting] // not sure how to do this :(
}
Run Code Online (Sandbox Code Playgroud)