无法使用单个值解码JSON

时间:2019-04-08 10:19:46

标签: swift

我正在尝试使用单个值解码JSON。该值没有任何密钥。

Alamofire
        .request(query, method: method, parameters: parameters)
        .validate()
        .responseJSON { response in
            print(response.request as Any)
        switch response.result {

        case .success(_):
            let data = response.data
            guard let data = data else {
                throw CustomExceptions.invalidJSON("No response found.")
            }
            do {
                let result = try APIHelper.deserialize(type: type, from: data)
                print(result)
            } catch {
                print(error)
            }
        case .failure(let error):
            print(error)
        }
    }

JSON文件仅包含一个没有任何键的值,即true。 在当前情况下,它是一个布尔值。结果应该是正确的。

但是我收到此错误。

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))

1 个答案:

答案 0 :(得分:1)

您无法使用def db_writer(moon, sun): print(counter) check = deaccent(sun).upper() if check in db.iloc[:]['names'].str.contains(check): i_close.iloc[I want the index of check where is True, 'code'] == to be the next row for i,y in zip(db.iloc[:]['names'], i_close.iloc[:]['names']): counter += 1 db_writer(i, y) 协议解码单个值,因为底层的Decodable API是用 no options 调用的,因此需要将集合类型作为根对象。

这正是错误消息

  

JSON文本不是以数组或对象开头,并且没有允许设置片段的选项

说,JSONSerialization不允许设置Decodable之类的任何选项