Swifty-JSON: 无法解析下划线
Swifty-JSON: unable to parse underscore
有一个 json 文件具有 ____
,其值类似于 "question": "11 ____________"
Swifty-json 无法解析并抛出错误 The data couldn’t be read because it isn’t in the correct format.
有什么办法可以处理这种情况吗?
JSON 响应 -
"typingQuestionArray": [
{
"head": "Type the number name for the following number.",
"question": "11 ____________",
"imageLink": "",
"correctAnswer": "eleven"
}
]
代码片段-
if let path = getPath(name: chapterString){
do {
let data = try Data(contentsOf: path, options: .alwaysMapped)
let json = try JSON(data: data)
print(json)
} catch let error {
print("parse error: \(error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
注意 - 不要怀疑 JSON 格式和 swift 代码。唯一的问题是 JSON.
中的 _____
json 验证的屏幕截图 -
我知道您正在使用 Swifty-JSON,但如果您不急于让它工作,看起来 JSONDecoder
可以完成工作:
let data = """
{
"head": "Type the number name for the following number.",
"question": "11 ____________",
"imageLink": "",
"correctAnswer": "eleven"
}
""".data(using: .utf8)!
你没有 post 你的结构,但我猜它看起来像这样:
struct Question: Codable {
let head: String
let question: String
let imageLink: String
let correctAnswer: String
}
解码:
let question = try! JSONDecoder().decode(Question.self, from: data)
然后可以打印问题:
print (question)
// Output:
///Question(head: "Type the number name for the following number.", question: "11 ____________", imageLink: "", correctAnswer: "eleven")
如果您对 JSONDecoder
的解决方案不感兴趣,请告诉我,我很乐意删除此答案。
此问题已通过将 Tab
替换为 Whitespace
.
得到修复
有一个 json 文件具有 ____
,其值类似于 "question": "11 ____________"
Swifty-json 无法解析并抛出错误 The data couldn’t be read because it isn’t in the correct format.
有什么办法可以处理这种情况吗?
JSON 响应 -
"typingQuestionArray": [
{
"head": "Type the number name for the following number.",
"question": "11 ____________",
"imageLink": "",
"correctAnswer": "eleven"
}
]
代码片段-
if let path = getPath(name: chapterString){
do {
let data = try Data(contentsOf: path, options: .alwaysMapped)
let json = try JSON(data: data)
print(json)
} catch let error {
print("parse error: \(error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
注意 - 不要怀疑 JSON 格式和 swift 代码。唯一的问题是 JSON.
中的_____
json 验证的屏幕截图 -
我知道您正在使用 Swifty-JSON,但如果您不急于让它工作,看起来 JSONDecoder
可以完成工作:
let data = """
{
"head": "Type the number name for the following number.",
"question": "11 ____________",
"imageLink": "",
"correctAnswer": "eleven"
}
""".data(using: .utf8)!
你没有 post 你的结构,但我猜它看起来像这样:
struct Question: Codable {
let head: String
let question: String
let imageLink: String
let correctAnswer: String
}
解码:
let question = try! JSONDecoder().decode(Question.self, from: data)
然后可以打印问题:
print (question)
// Output:
///Question(head: "Type the number name for the following number.", question: "11 ____________", imageLink: "", correctAnswer: "eleven")
如果您对 JSONDecoder
的解决方案不感兴趣,请告诉我,我很乐意删除此答案。
此问题已通过将 Tab
替换为 Whitespace
.