Swift 可解码 - 解码具有 Base64 编码字符串的 json
我正在尝试解码编码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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论