【发布时间】:2017-02-20 19:43:36
【问题描述】:
我真的很难确定 SwiftyJson 中返回的特定值;希望有人能帮我解释一下。
我想查看预先确定的单词“apple”是否与从 JSON 响应中收到的任何单词匹配。
如果匹配,则会显示一条消息,用户要么选择进入下一个级别,要么返回主屏幕。
如果没有匹配,则会显示一条消息,用户必须继续播放或取消播放。
我想对游戏不同级别的多个单词执行此操作。
第一级:将“apple”匹配到任何接收到的 JSON 响应。
第二级:将“计算机”匹配到任何接收到的 JSON 响应。
第三级:将“电话”或“电话”或“iPhone”或“Android”或上述任何或所有内容与收到的任何 JSON 响应匹配。
因此,基本上,我可以获得所有 JSON 响应,但我很难找出如何设置以确定是否返回了特定的、预定义的 JSON 响应。
我用另一个帖子到处找了几个星期,但无济于事:(
JSON 响应
{
"responses" : [
{
"labelAnnotations" : [
{
"mid" : "\/m\/01m2v",
"score" : 0.9245476,
"description" : "computer keyboard"
},
{
"mid" : "\/m\/01c648",
"score" : 0.7945268,
"description" : "laptop"
},
{
"mid" : "\/m\/01mfj",
"score" : 0.74227184,
"description" : "computer hardware"
},
{
"mid" : "\/m\/0541p",
"score" : 0.7062791,
"description" : "multimedia"
},
{
"mid" : "\/m\/07c1v",
"score" : 0.7039645,
"description" : "technology"
},
{
"mid" : "\/m\/03gq5hm",
"score" : 0.69323385,
"description" : "font"
},
{
"mid" : "\/m\/0bs7_0t",
"score" : 0.6724673,
"description" : "electronic device"
},
{
"mid" : "\/m\/01vdm0",
"score" : 0.66489816,
"description" : "electronic keyboard"
},
{
"mid" : "\/m\/0121tl",
"score" : 0.60392517,
"description" : "electronic instrument"
},
{
"mid" : "\/m\/0h8n5_7",
"score" : 0.5834592,
"description" : "laptop replacement keyboard"
}
]
}
]
}
显示所有 JSON 响应的代码
// Use SwiftyJSON to parse results
let json = JSON(data: dataToParse)
let errorObj: JSON = json["error"]
// Parse the response
print(json)
let responses: JSON = json["responses"][0]
// Get label annotations
let labelAnnotations: JSON = responses["labelAnnotations"]
let numLabels: Int = labelAnnotations.count
var labels: Array<String> = []
if numLabels > 0 {
var labelResultsText:String = "Labels found: "
for index in 0..<numLabels {
let label = labelAnnotations[index]["description"].stringValue
labels.append(label)
}
for label in labels {
// if it's not the last item add a comma
if labels[labels.count - 1] != label {
labelResultsText += "\(label), "
} else {
labelResultsText += "\(label)"
}
}
self.labelResults.text = labelResultsText
} else {
self.labelResults.text = "No labels found"
}
编辑
我显然无法回答我自己的问题,我会发布一个编辑,因为我认为这是一个更好的解决方案,但 @pierce 的一个词相当不错,不是很多;它只是不适用于游戏设置应用程序。
所以,我创建了一个新的 NSObject,创建了一个
static var _words: [[String]] = [
["apple", "computer", "beer"]]
然后
func checkAnnotations(annotations: [Annotation]) -> Bool {
var isMatched = false
let searchWords = self.words
for searchWord in searchWords {
for annotation in annotations {
if searchWord == annotation.descriptionString {
isMatched = true
break
}
}
if isMatched {
break
}
}
return isMatched
}
然后创建了一个函数来处理关卡状态,
最后将其与视图控制器中的 JSON 响应和高级级别(如果匹配)进行比较
// Get JSON key value
let labelAnnotations = responses["labelAnnotations"].arrayValue
let annotationObjects: [Annotation] = labelAnnotations.flatMap({ annotationDictionary in
if let mid = annotationDictionary["mid"].string,
let score = annotationDictionary["score"].double,
let description = annotationDictionary["description"].string {
let annotation = Annotation(mid: mid, score: score, descriptionString: description)
return annotation
}
return nil
})
//print(annotationObjects)
let searchString = LevelState.shared.words[0]
print("Level \(LevelState.shared.level), looking for: \(searchString)")
var isMatched = LevelState.shared.checkAnnotations(annotations: annotationObjects)
if isMatched {
LevelState.shared.advance()
}
let alertTitle = isMatched ? "Congrats! You got \(searchString)" : "Keep looking for \(searchString)"
//let translationResult = "Translated: \(levelDescription) to \(translatedText)"
let alertController = UIAlertController(title: alertTitle, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
self.prepareForNewLevel()
})
}
【问题讨论】:
-
我对几件事有点不清楚:首先 - 你为什么要在
labels数组的每个条目中添加一个逗号?其次,您似乎将labels设置为JSON中的每个description...因此,如果您需要将这些值与用户输入的某些单词进行比较,过滤器会为您工作吗?let matches = labels.filter { $0.contains(user_entered_word) } -
只有在返回多个 JSON 响应时才使用逗号;如果我预先定义了一个想要的单词,那显然是没有必要的。如果我可以使用“过滤器”,那么可以确定,正如我在其他示例中看到的那样,但我不确定如何在我的代码中进行设置:/ 一分钟我觉得我真的理解 Json,下一分钟我完全被解析弄糊涂了。 @皮尔斯
-
我在下面为您添加了答案。希望对你有帮助
-
我的回答有问题吗?你为什么不接受?我只是好奇
-
阅读我上面帖子中的编辑。这还不错,但我认为我的解决方案更全面。我无法发布答案@Pierce
标签: ios json swift swifty-json