如何在ios应用程序中检索firebase数据库中的数据?我的JSON数据如下所示,我希望得到与questiontext关联的每个值,A…E
JSON数据如下:如何在ios应用程序中检索firebase数据库中的数据?我的JSON数据如下所示,我希望得到与questiontext关联的每个值,A…E,ios,swift,firebase,firebase-realtime-database,nsdictionary,Ios,Swift,Firebase,Firebase Realtime Database,Nsdictionary,JSON数据如下: { "Questions": { "1": { "questiontext": "Which choice best describes what happens in the\npassage?", "A": "One character argues with another character who intrudes on her home.", "B": "One cha
{
"Questions": {
"1": {
"questiontext": "Which choice best describes what happens in the\npassage?",
"A": "One character argues with another character who intrudes on her home.",
"B": "One character receives a surprising request from\nanother character",
"C": "One character reminisces about choices she has\nmade over the years.",
"D": "One character criticizes another character for\npursuing an unexpected course of action.",
"E": "null"
},
"2": {
"questiontext": "Which choice best describes the developmental pattern of the passage?",
"A": "A careful analysis of a traditional practice",
"B": "A detailed depiction of a meaningful encounter",
"C": "A definitive response to a series of questions",
"D": "A cheerful recounting of an ammusing anecdote",
"E": "null"
}
}
}
您可能会发现您的结构存在一些挑战,因此将来可能需要进行更改 由于您的问题非常具体,关于包含的结构,下面介绍如何访问问题1的值,回答A(或B、C、D等) 输出是
One character argues with another character who intrudes on her home.
现在,如果您想获得所有子节点A-E,那么下面是代码
let q1Ref = self.ref.child("Questions").child("1")
q1Ref.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
let value = snap.value as! String
print("Answer \(key) is \(value)")
}
})
输出将是
Answer A is One character argues with another character who intrudes on her home.
Answer B is One character receives a surprising request from\nanother character
Answer C is One character reminisces about choices she has\nmade over the years.
Answer D is One character criticizes another character for\npursuing an unexpected course of action.
Answer E is null
发表评论;这假设您的Firebase根引用定义如下:
class ViewController: UIViewController {
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference()
您的firebase结构键是1、2、3(如在数组中*),而不是“1”、“2”、“3”
您可以通过查看Firebase控制台并查看键是否用引号括起来来区分它们之间的差异;1不同于“1”
最好使用.childByAutoId来创建父节点键,而不是数字(数组)
*应避免在Firebase中使用阵列。请参见谢谢您的回答,但我需要直接从Firebase访问此答案。此答案的作用是什么?i、 e.它将如何从firebase加载指定的节点,并使OP能够访问与A、B、C等节点相关的谨慎答案?我尝试了上述代码,但不起作用。我尝试通过在中间添加打印行来调试。它似乎一直工作到q1Ref(因为我可以用q1Ref打印firebase的https),然后它无法从这里输入代码:-q1Ref.observeSingleEvent(值:,带:{快照in@AppManiac上述代码的工作方式与直接从工作项目复制和粘贴的代码相同。我的答案中包含的输出是从XCode控制台实际复制和粘贴的结果。一个区别是,您的Firebase结构与您的问题中包含的结构略有不同。另一个问题是Firebase如何重新创建定义了f。我将它定义为'ref'的类变量,这样我就可以使用self.ref访问我的Firebase根引用。
Answer A is One character argues with another character who intrudes on her home.
Answer B is One character receives a surprising request from\nanother character
Answer C is One character reminisces about choices she has\nmade over the years.
Answer D is One character criticizes another character for\npursuing an unexpected course of action.
Answer E is null
class ViewController: UIViewController {
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference()