ios - 将 JSON 字符串转换为模型对象失败

标签 ios json swift codable

我有一个来自服务器的非常大的 JSON,但到模型对象的转换失败。

我已经尝试了很多相同的修复方法,但没有一个有效

所以我提取了一个带有一些反射(reflect)模型的值的键

它在大 JSON 中的样子(appConfig 是众多键之一)

\"appConfig\":\"{\\\"launcherAfterLogin\\\":\\\"ma.dista.activities.jobs.JobListingActivity\\\",\\\"logging\\\":true,\\\"crashReport\\\":true,\\\"defaultLanguage\\\":\\\"en-US\\\"}\"

当我将其提取出来并将其分配给 String 时,它看起来如何?变量

{\"appConfig\":\"{\\\"launcherAfterLogin\\\":\\\"activities.jobs.JobListingActivity\\\",\\\"logging\\\":true,\\\"crashReport\\\":true,\\\"defaultLanguage\\\":\\\"en-US\\\"}\"}

我创建了一个模型来将此 JSON 映射到

struct Config: Decodable {
    let appConfig: AppConfig
}

struct AppConfig: Decodable {
    let launcherAfterLogin: String?
    let logging: Bool?
    let crashReport: Bool?
    let defaultLanguage: String?
    let updateUrl: String?
    let imageUploadAsPDF: Bool?
}

然后我使用下面的代码将 JSON 映射到模型

let jsonStringModified = "{\"appConfig\":\"{\\\"launcherAfterLogin\\\":\\\"activities.jobs.JobListingActivity\\\",\\\"logging\\\":true,\\\"crashReport\\\":true,\\\"defaultLanguage\\\":\\\"en-US\\\"}\"}"

let jsonData = jsonStringModified.data(using: .utf8)!

do {
    let jsonModel = try JSONDecoder().decode(Config.self, from: jsonData)
    print(jsonModel)
} catch let error as NSError {
    print(error)
}

现在,当我运行它时,它会因此错误而崩溃

Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode Dictionary but found a string/data instead." UserInfo={NSCodingPath=( "CodingKeys(stringValue: \"appConfig\", intValue: nil)" ), NSDebugDescription=Expected to decode Dictionary but found a string/data instead.}

我还尝试将此数据值转换为 JSONSerialized Dictionary,然后返回 JSONSerialized Data,然后将其馈送到 JSONDecoder

do {
    if let jsonSerialised = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? [String: Any] {

        let jsonSerialisedUpdated = try JSONSerialization.data(withJSONObject: jsonSerialised, options: .prettyPrinted)

        let jsonModel = try JSONDecoder().decode(Config.self, from: jsonData)

        print(jsonModel)
    }

} catch let error as NSError {
    print(error)
}

但这里也面临同样的错误

此外,在我的 Big JSON 中,每次运行时,只有这个键 (appConfig) 在所有键中崩溃

更新:

实际上这整个Config型号为 String其中有各种键,例如 appConfig 等,我正在创建一个解析器来转换此 Config作为字符串到 Config作为模型

struct BiggerConfig: Decodable {
    let otherConfig: CustomType
    let config: String // I want to convert this String to Config type which has keys like appConfig inside it
}

config上面实际上是一个 JSON 但它是 String来自服务器,我想为其创建一个解析器

最佳答案

问题在于您的 JSON 字符串的结构与您的想法不同。如果我们删除所有转义,您拥有的 JSON 是这样的:

{"appConfig" : "{\"launcherAfterLogin\":\"activities.jobs.JobListingActivity\",\"logging\":true,\"crashReport\":true,\"defaultLanguage\":\"en-US\"}"}

appConfig 键的值是一个包含另一个转义 JSON 字符串的字符串。

要解决此问题,请修复传入的 JSON 或将 appConfig 解码为字符串,然后将字符串解码为 Config 对象

关于ios - 将 JSON 字符串转换为模型对象失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165054/

相关文章:

ios - React Native在调用3方库后失去了对iOS状态栏的控制

ios - 在 iOS 中将通知从模型发送到 Controller

ios - 禁用时分隔符出现在 uitableview 中

java - 使用 HttpURLConnection 读取 REST 请求输出

python - 使用 Python 删除 json 文件中的新换行符。

ios - 连接到 iBeacon

ios - 长按后 Swift UiTableViewCell 重置

ios - Xcode Instruments 分配中的所有堆分配和所有匿名分配是什么?

javascript - 是否有一个 react native 包来检索附近的 iOS 无线 SSID?

python - 在 peewee (Flask) 的查询中包含 OneToMany 关系