如何快速解析Firebase FDatasnapshot json数据 [英] how parsing Firebase FDatasnapshot json data in swift
问题描述
从Firebase获取数据时遇到问题.
I'm having issue getting data from Firebase.
模式是
{
title: "dog",
images: {
main: "dog.png",
others: {
0: "1.png",
1: "2.png",
2: "3.png"
}
}
}
如何将FDataSnapshot解析为快速模型?
how can i parse FDataSnapshot to swift model??
推荐答案
Firebase是NoSQL JSON数据库,没有架构和表.数据以带有节点的树"结构存储;父母和孩子.
Firebase is a NoSQL JSON database and has no schema and no tables. Data is stored with a 'tree' structure with nodes; parents and children.
您无需解析Firebase JSON数据即可访问它,您可以直接访问它.
You don't need to parse Firebase JSON data to access it, you can access it directly.
FDataSnapshots包含一个.key,它是Firebase和.value中的父键. .Value可以包含一个节点,也可以包含多个节点.该值将具有key:value对,代表快照中的数据
FDataSnapshots contain a .key, which is it's parent key in Firebase and .value. .Value may contain one node, or multiple nodes. The Value will have key:value pairs representing the data within the snapshot
因此,在您的示例中,您将具有这样的Firebase结构
So for your example you will have a Firebase structure like this
dogs
dog_id_0
title: "dog"
type: "Alaskan Malamute"
images:
main: "dog.png"
others:
0: "1.png"
1: "2.png"
dog_id_1
title: "another dog"
type: "Boxer"
images:
main: "another_dog.png"
others:
0: "3.png"
1: "4.png"
因此,假设您想一次在每个dog_id_x节点中读取一个并打印一些值.
So, say you want to read in each dog_id_x node one at a time and print some values.
var ref = Firebase(url:"https://your-app.firebaseio.com/dogs")
ref.observeEventType(.ChildAdded, withBlock: { snapshot in
println(snapshot.value.objectForKey("title"))
println(snapshot.value.objectForKey("type"))
})
这将输出
dog
Alaskan Malamute
another dog
Boxer
dog_id_0和dog_id_1是使用Firebase childByAutoId创建的节点名称.
The dog_id_0 and dog_id_1 are node names created with the Firebase childByAutoId.
您可以轻松地创建一个Dog类,然后将其传递给FDataSnapshot,该FDataSnapshot将根据快照中的数据填充该类.
You could just as easily create a Dog class, and pass it the FDataSnapshot which will populate the class from the data within the snapshot.
这篇关于如何快速解析Firebase FDatasnapshot json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!