Swift 可解码 - 解码具有 Base64 编码字符串的 json

发布于 2025-01-19 05:48:15 字数 833 浏览 9 评论 0原文

我正在尝试解码编码base64的JSON响应。

示例JSON:

{ "contract": "JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JTcGFjZS9EZXZpY2V", "isSuccess": true }
  • 如何将base64解码为字符串?

我的代码&模型:

struct ruleModel: Codable {
    let contract: String?
    let isSuccess: Bool?
 }


let decoder = JSONDecoder()
let response = try decoder.decode(ruleModel.self, from: result! as! Data)
print(response)

解码错误:

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0})))

I am trying to decode JSON response that has encoded base64.

Example JSON:

{ "contract": "JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JTcGFjZS9EZXZpY2V", "isSuccess": true }
  • How can I decode base64 to string?

My code & Model:

struct ruleModel: Codable {
    let contract: String?
    let isSuccess: Bool?
 }


let decoder = JSONDecoder()
let response = try decoder.decode(ruleModel.self, from: result! as! Data)
print(response)

Decoding Error:

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0})))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文