Ios 如何从本地JSON正确解析JSON?
我很难正确解析本地JSON文件中的JSON,将其存储到数组中,然后在视图中显示它 我的应用程序没有崩溃。屏幕上的标签(4个按钮和1个标签)不会改变值。他们只是得到我在“问题1”,“问题2”中添加的样本值,等等 如何让它显示JSON文件中的值?看起来像Ios 如何从本地JSON正确解析JSON?,ios,swift,Ios,Swift,我很难正确解析本地JSON文件中的JSON,将其存储到数组中,然后在视图中显示它 我的应用程序没有崩溃。屏幕上的标签(4个按钮和1个标签)不会改变值。他们只是得到我在“问题1”,“问题2”中添加的样本值,等等 如何让它显示JSON文件中的值?看起来像 你能提供更多的细节吗?你能描述一下到目前为止你所做的调试吗?事情进展到了什么程度?哪些特定的方法/行肯定不能像您预期的那样工作?打印错误吗?您想从json文件解析jason数据,对吗?@AaronBrager感谢您的回复。我还没有从调试中得出任何有
你能提供更多的细节吗?你能描述一下到目前为止你所做的调试吗?事情进展到了什么程度?哪些特定的方法/行肯定不能像您预期的那样工作?打印错误吗?您想从json文件解析jason数据,对吗?@AaronBrager感谢您的回复。我还没有从调试中得出任何有用的结论。我只是不明白为什么questionObjects NSMutableOrderedSet没有正确填充。我制作了一个视频来告诉你到底发生了什么。
/* Question label storyboard outlet */
@IBOutlet weak var questionLabel: UILabel!
// Answer button storyboard outlets
@IBOutlet weak var topLeftButton: UIButton!
@IBOutlet weak var topRightButton: UIButton!
@IBOutlet weak var bottomLeftButton: UIButton!
@IBOutlet weak var bottomRightButton: UIButton!
/* Questions Set */
var questionObjects = NSMutableOrderedSet(capacity: 100)
override func viewDidLoad() {
super.viewDidLoad()
getQuestionsFromJSONFile()
displayQuestion(0, jsonObjectsSet: questionObjects)
let string = "some Random Question"
questionLabel.text = string.capitalizeFirstCharacter
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getQuestionsFromJSONFile() {
var questionsJSONData: NSData? = nil
do {
let pathToQuestionsJSONFile = try NSBundle.mainBundle().pathForResource("Trivia_100-199", ofType: "json")
do {
questionsJSONData = try NSData(contentsOfFile: pathToQuestionsJSONFile!, options: NSDataReadingOptions.DataReadingMappedIfSafe)
let questionsJSONContent = JSON(data: questionsJSONData!)
if let object : [String:[String:AnyObject]] = questionsJSONContent.object as? [String:[String:AnyObject]] {
parseJSONContentIntoObjects(object)
}
} catch let error as NSError {
print(error.localizedDescription)
}
/*
if let questionsJSONContent = JSON(data: questionsJSONData!) as? [String: [String : AnyObject]] {
parseJSONContentIntoObjects(questionsJSONContent) // PSE: Potential Source of Error
}
*/
} catch let error as NSError {
print(error.localizedDescription)
print(error.localizedFailureReason)
print(error.localizedRecoveryOptions)
print(error.localizedRecoverySuggestion)
}
}
func parseJSONContentIntoObjects(jsonData: [String: [String : AnyObject]]) {
var questionHasAProperAnswer = false // indicator to determine if a proper answer was provided by the API for this specific question
let jsonObjects = NSMutableOrderedSet(capacity: 100)
for (questionAnswer,questionPropertiesSubJson):(String,[String:AnyObject]) in jsonData {
if let questionObject = Question(questionObjectSubJson: questionPropertiesSubJson) as? Question { // All parsing code is in the model file (Question.swift)
if let answerText = questionObject.questionAnswer as? String {
let questionExclusionKeywords = ["the Earth probably", "boring", "uninteresting", "unremarkable", "we do not know", "nothing", "remarkable", "a number for which", "missing a fact"]
for questionExclusionKeyword in (questionExclusionKeywords as? [String])! {
questionHasAProperAnswer = !answerText.containsString(questionExclusionKeyword)
/*
if questionHasAProperAnswer {
jsonObjects.addObject(questionObject)
}
}
if answerText.containsAny(questionExclusionKeywords) {
jsonObjects.addObject(questionObject)
}
*/
jsonObjects.addObject(questionObject)
}
}
}
questionObjects = jsonObjects
if questionObjects.count == 0 {
let fillerObject = Question(questionText: "Some question", questionAnswer: 1234, questionFalseAnswer1: "333", questionFalseAnswer2: "444", questionFalseAnswer3: "555")
questionObjects.addObject(fillerObject)
}
}
}
func displayQuestion(questionIndex: Int, jsonObjectsSet: NSMutableOrderedSet) {
if let specificQuestionObject = jsonObjectsSet.objectAtIndex(questionIndex) as? Question {
if let questionText = specificQuestionObject.questionText {
questionLabel.text = questionText
}
if let questionAnswer = specificQuestionObject.questionAnswer as? String {
let capitalizedQuestionAnswer = questionAnswer.capitalizeFirstCharacter
topLeftButton.titleLabel?.text = capitalizedQuestionAnswer
}
bottomLeftButton.titleLabel?.text = "Some false answer"
topRightButton.titleLabel?.text = "Some other false answer"
bottomRightButton.titleLabel?.text = "Another random false answer"
}
}