我的查询返回FirebaseObjects列表:
IReadOnlyCollection<Firebase.Xamarin.Database.FirebaseObject<object>> items = await firebase.Child("product").OnceAsync<object>();
但我想这样做:
Product product = await firebase.Child("product").OnceAsync<Product>();
我可以更改我的查询以使其正常工作,还是这就是它的工作原理? 我的意思是,我是否总能找回一个列表,还是可以直接反序列化到我的Product对象?
json的完整结构
{
"product1": {
"pname": "Peppermint Kiss",
"teaType": "Black Tea",
"company": "PersnickeTea",
"packSize": {
"teabag": [
{
"qty": 1,
"price": 1.5,
"tImage": "singleCup.jpg"
},
{
"qty": 5,
"tinPrice": 6.95,
"tImage": "blackTeaTin.jpg"
},
{
"qty": 8,
"price": 10,
"tImage": "blackTeabox.jpg"
},
{
"qty": 12,
"boxPrice": 14.95,
"tImage": "trayBox.jpg"
}
],
"looseLeaf": [
{
"qty": 46,
"price": 7.95,
"tImage": "mintKissPak.jpg"
}
]
},
"ingredients": [
"black tea",
"peppermint leaves",
"candy pieces",
"organic peppermint oil",
"natural creme flavor",
"raspberry leaves",
"red clover",
"anise seed"
],
"prepMethod": [
"infuser",
"teabags",
"strainer",
"iced"
]
},
"product2": {
"pname": "Spearmint Kiss",
"teaType": "Black Tea",
"company": "PersnickeTea",
"packSize": {
"teabag": [
{
"qty": 1,
"price": 1.5,
"tImage": "singleCup.jpg"
},
{
"qty": 5,
"tinPrice": 6.95,
"tImage": "blackTeaTin.jpg"
},
{
"qty": 8,
"price": 10,
"tImage": "blackTeabox.jpg"
},
{
"qty": 12,
"boxPrice": 14.95,
"tImage": "trayBox.jpg"
}
],
"looseLeaf": [
{
"qty": 46,
"price": 7.95,
"tImage": "mintKissPak.jpg"
}
]
},
"ingredients": [
"black tea",
"peppermint leaves",
"candy pieces",
"organic peppermint oil",
"natural creme flavor",
"raspberry leaves",
"red clover",
"anise seed"
],
"prepMethod": [
"infuser",
"teabags",
"strainer",
"iced"
]
},
"product3": {
"pname": "Mintymint Kiss",
"teaType": "Black Tea",
"company": "PersnickeTea",
"packSize": {
"teabag": [
{
"qty": 1,
"price": 1.5,
"tImage": "singleCup.jpg"
},
{
"qty": 5,
"tinPrice": 6.95,
"tImage": "blackTeaTin.jpg"
},
{
"qty": 8,
"price": 10,
"tImage": "blackTeabox.jpg"
},
{
"qty": 12,
"boxPrice": 14.95,
"tImage": "trayBox.jpg"
}
],
"looseLeaf": [
{
"qty": 46,
"price": 7.95,
"tImage": "mintKissPak.jpg"
}
]
},
"ingredients": [
"black tea",
"peppermint leaves",
"candy pieces",
"organic peppermint oil",
"natural creme flavor",
"raspberry leaves",
"red clover",
"anise seed"
],
"prepMethod": [
"infuser",
"teabags",
"strainer",
"iced"
]
}
}
答案 0 :(得分:1)
您的查询返回对象列表,因为您在不添加任何过滤器的情况下查询主节点。那是&#34;同样的&#34;如上所述
SELECT * FROM PRODUCT;
在一个临时数据库中。
你做不到
Product product = await firebase.Child("product").OnceAsync<Product>();
当您有来自请求的产品列表时。如果您只想选择一个,则需要按键,子项或值进行过滤。
但你可以这样做:
var products = await firebase.Child("product").OnceAsync<Product>();
您将拥有一系列已经反序列化的产品。
使用此:
var product = await firebase
.Child("product")
.Child("your-product-id")
.OnceAsync<Product>();
如果您将productId定义为节点的关键,则获取单个产品。
将结构移动到原始帖子....
答案 1 :(得分:0)
我能够找到适合我的解决方案。
var items = await firebase.Child("products").OnceAsync<object>();
foreach( var item in items) {
product = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>.(item.Object.ToString());
}
但我提出了apineda的解决方案。该查询有效但在我尝试使用该产品时会抛出异常。
var product = await firebase
.Child("products")
.Child("product1")
.OnceAsync<Product>();
但我认为我需要进行x#firebase调用以获取所有产品,而我的解决方案可以在一次调用中获取所有产品。