标签: codable

如何在 Swift 中从 JSON 解码大量数字

我如何像这样解析 JSON:

let json = "{\"key\":18446744073709551616}"

struct Foo: Decodable {
    let key: UInt64
}

let coder = JSONDecoder()
let test = try! coder.decode(Foo.self, from: json.data(using: .utf8)!)
Run Code Online (Sandbox Code Playgroud)

问题是这个数字对于UInt64. 我知道 Swift 中没有更大的整数类型。

Parsed JSON number <18446744073709551616> does not fit in UInt64
Run Code Online (Sandbox Code Playgroud)

我不介意把它作为Stringor Data,但这是不允许的,因为JSONDecoder知道它应该是一个数字:

Expected to decode String but found a number instead.
Run Code Online (Sandbox Code Playgroud)

parsing json swift codable

0
推荐指数
1
解决办法
1117
查看次数

JSON 解码为 Swift 中的递归枚举并从其编码

我有一个 JSON 文件,看起来像这样:

{
    "items" : [
        { "name": "a name", "version": "a version" },
        { "version": "a 2nd version" },
        {
            "any_of": [
                { "name": "some name" },
                { "name": "some other name", "version": "some other version" },
                [
                    { "name": "another name" },
                    { "version": "another version" },
                    {
                        "any_of": [
                            [
                                { "version": "some version" },
                                { "version": "some version" }
                            ],
                            { "version": "yet another version" }
                        ]
                    }
                ]
            ]
        },
        {
            "any_of" : [ …
Run Code Online (Sandbox Code Playgroud)

recursion enums json swift codable

0
推荐指数
1
解决办法
1045
查看次数

如何解码许多可解码 Swift 中的单个异常属性?

我有一个符合可解码的结构。它有 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 swift codable decodable encodable

0
推荐指数
1
解决办法
262
查看次数

无法解码 JSON,因为模型是可识别的

我有一个像这样的 JSON

[
 {
   "name": "car",
   "color": "red"
 },
 {
   "name": "bike",
   "color": "blue"
 },
  ... etc
]
Run Code Online (Sandbox Code Playgroud)

我在解码过程中可以使用这个模型

struct Element:Codable {
  var name:String?
  var color:String?
}
Run Code Online (Sandbox Code Playgroud)

稍后在代码中我有ScrollView这样的

 ScrollView(.vertical, showsIndicators: false) {
        LazyVGrid(columns: gridItemLayout, alignment: .center, spacing: 10, content: {
          ForEach(allElements) { element in
Run Code Online (Sandbox Code Playgroud)

allElements是一个数组Element

因为我allElements在那里使用,所以我必须使其Element可识别......

struct Element:Codable, Identifiable {
  var id = UUID()
  var name:String?
  var color:String?
}
Run Code Online (Sandbox Code Playgroud)

现在它无法解码 JSON,因为 JSON 没有id.

我尝试添加

"id" : "",
Run Code Online (Sandbox Code Playgroud)

JSON 上的所有元素均未成功。 …

json swift codable

0
推荐指数
1
解决办法
928
查看次数

通过 Codable 将 JSON 解析为数组

我有以下 JSON:

\n
[\n{\n "id": 1,\n "type": "Feature",\n "geometry": {\n     "type": "Point",\n     "coordinates": [\n         37.5741167,\n         55.7636592\n     ]\n },\n "properties": {\n     "hintContent": "\xd0\xbf\xd0\xb5\xd1\x80\xd0\xb5\xd1\x83\xd0\xbb\xd0\xbe\xd0\xba \xd0\x92\xd0\xbe\xd0\xbb\xd0\xba\xd0\xbe\xd0\xb2, 13\xd1\x811",\n     "balloonContentHeader": "\xd0\xbf\xd0\xb5\xd1\x80\xd0\xb5\xd1\x83\xd0\xbb\xd0\xbe\xd0\xba \xd0\x92\xd0\xbe\xd0\xbb\xd0\xba\xd0\xbe\xd0\xb2, 13\xd1\x811"\n }\n]\n
Run Code Online (Sandbox Code Playgroud)\n

我正在尝试使用 JSONDecoder:

\n
struct Point : Codable {\n    let id: Int\n    let type: String\n    let properties: Properties\n}\nstruct Properties : Codable {\n    let hintContent: String\n    let balloonContentHeader: String\n}\nstruct Points : Codable {\n    var data : [Point]\n}\n\n\n\nfunc parse(fileName: String) {\n\n    let url = Bundle.main.url(forResource: fileName, withExtension: "json")\n    let data = try? Data(contentsOf: …
Run Code Online (Sandbox Code Playgroud)

json swift codable

0
推荐指数
1
解决办法
210
查看次数

我正在快速使用编码键,但出现此错误

我需要读取放在我的包中的 CSV 文件,当我解码每一行时,它有不同的列名称,我想将其映射到我的自定义对象结构上,因为我试图使用编码键,但它不起作用,请看一下,让我知道是否需要更改任何内容

在此图像中,错误显示多个枚举原始类型,我不知道这是什么意思! 谢谢如果有人可以帮忙

enums swift codable

-2
推荐指数
1
解决办法
1318
查看次数

如何将任意数据转化为数据

我想Any?用 Swift 保存(到 FileManager)。

它可以是一切,也可以是不符合 Codable 的类型(例如SCNNodes)。

我想将其包装到 Data 中。我知道,我不能用PropertyListEncoder.

您能让我轻松理解它是如何工作的吗?

file-manager any swift codable

-5
推荐指数
1
解决办法
134
查看次数

标签 统计

codable ×7

swift ×7

json ×5

enums ×2

any ×1

decodable ×1

encodable ×1

file-manager ×1

parsing ×1

recursion ×1