【问题标题】:Swift JSON parsing with no particular structure没有特定结构的 Swift JSON 解析
【发布时间】:2022-12-10 14:23:52
【问题描述】:

我有一个这种类型的 JSON 对象,非常实用的 do 循环(对于每个键 => 值)
就像 JSON 的 JSON

但我无法在 Swift 中对其进行解码。
我想知道这种类型的数据是否可以在 Swift 中解码。
如果这对你来说很容易,你能帮忙解码吗?例如在数组中。

{"1":{"1":"一些文本"},"2":{"1":"一些文本","2":"一些文本","3":"一些文本","4 ":"一些文本"},"3":{"1":"一些文本","2":"一些文本","3":"一些文本"},"4":{"1": “一些文字”,“2”:“一些文字”},“5”:{“1”:“一些文字”},“6”:{“1”:“一些文字”,“2”:“一些文字","3":"一些文字"}}

        let jsonDict = json_text
        let jsonDictData = jsonDict.data(using: .utf8)!

        let object = try? JSONSerialization.jsonObject(
            with: jsonDictData,
            options: []
        )

        // Cast to a Swift Dictionary
        let dict = object as? [AnyHashable:Any]

        // Cast to an NSDictionary
        let nsDict = object as? NSDictionary
        print(nsDict)

它返回

【问题讨论】:

    标签: json parsing


    【解决方案1】:

    最后我自己解析了。 使用 .components(separatedBy: "},") 和.components(separatedBy:“”,“”)

    并用 .replacingOccurrences(of: pattern, with: "") 清除一些字符 模式 ID 例如 pattern = ""1":""

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多