我確實在確定SwiftyJson中返回的特定值時遇到了問題;希望有人能幫我解釋一下。在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
}
然後創建一個函數來處理的水平狀態,
最後將其與View Controller中的JSON響應進行比較並推進d級如果匹配
// 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'數組的每個條目添加一個逗號?其次,它看起來像從'JSON'設置'labels'爲每個'description' ...所以如果您需要將這些值與用戶輸入的某個單詞進行比較,會爲您篩選工作嗎? 'let matches = labels.filter {$ 0.contains(user_entered_word)}' – Pierce
只有在返回多個JSON響應時纔會使用逗號;如果我預先定義了一個單一的期望單詞,顯然不需要這樣做。如果我可以使用「過濾器」,那麼肯定,正如我在其他例子中看到的,但我不知道如何設置在我的代碼中:/一分鐘,我覺得我真的明白Json,下一分鐘我是解析它完全困惑。 @Pierce – pythlang
我在下面爲你添加了一個答案。希望它有幫助 – Pierce