im making quiz app and I want to download questions from server in JSON file, parse it and make question object that I will present. I did it so now I want to make an app that will be creating JSON file and upload it to the server, I want to it looks like this
我将从文本字段获取所有信息并将其保存在 JSON 文件中,如下所示(带有顺序值)
[
{
"question":"If you want to create a custom class which can be displayed on the view, you can subclass UIView.",
"answers":["True", "False"],
"correctIndex":0,
"module":3,
"lesson":0,
"feedback":"Subclassing UIView gives your class the methods and properties of a basic view which can be placed onto the view."
}
]
swift 中是否有任何具有我可以使用的功能的框架?
还是我必须手动制作?如果手动如何保存 JSON 文件?
您可以使用 JSONSerialization 类来实现此目的。请参阅下面在 Playground 中编写的代码片段
import Foundation
// Dictionary containing data as provided in your question.
var dictonary : [String : Any] = ["question":"If you want to create a custom class which can be displayed on the view, you can subclass UIView.",
"answers":["True", "False"],
"correctIndex":0,
"module":3,
"lesson":0,
"feedback":"Subclassing UIView gives your class the methods and properties of a basic view which can be placed onto the view."
]
if let jsonData = try JSONSerialization.data(withJSONObject: dictonary, options: .init(rawValue: 0)) as? Data
{
// Check if everything went well
print(NSString(data: jsonData, encoding: 1)!)
// Do something cool with the new JSON data
}
如果您在 Xcode Playground 中运行此代码,您可以看到以 JSON 格式打印的数据
获得 JSON 后,您可以使用您选择的网络库将数据发送到服务器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)