ios - 如何在 ios 应用程序中检索 firebase 数据库中的数据?我的 JSON 数据如下所示,我想获取与问题文本 A...E 关联的每个值

标签 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 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 等)的值

let q1Ref = self.ref.child("Questions").child("1")
let q1Ref_a_ref = q1Ref.child("A")
q1Ref_a_ref.observeSingleEvent(of: .value, with: { snapshot in
    let answer = snapshot.value as! String
    print(answer)
})

输出是

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 root ref 是这样定义的:

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 中应避免使用数组。参见 Arrays

关于ios - 如何在 ios 应用程序中检索 firebase 数据库中的数据?我的 JSON 数据如下所示,我想获取与问题文本 A...E 关联的每个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48181448/

相关文章:

objective-c - UITabBar.appearance().barTintColor 不适用于 Xcode 中的自定义颜色

ios - FirebaseFirestore 可以在 iOS 应用扩展中使用吗?

ios - 自定义 View TextField 如何计算TextField总和

iOS UIView 动画 move 到另一个 UIView

ios - 在设备而不是模拟器上运行 sprite-kit 项目时将每秒帧数设置为 30

objective-c - UIModalTransitionStyleFlipHorizo​​ntal 不起作用

ios - Swift - 旋转 UITextView 自动调整大小问题

javascript - Firebase 的云函数 : Increment Counter

firebase - super 基本的 Firestore 安全规则不起作用

javascript - 取消事务时在后续 storekit.init 上出现多个 "Cannot connect to iTunes store"消息 -Cordova/Phonegap