我的查询返回一个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"
]
}
}发布于 2017-05-17 18:17:19
我找到了一个对我有用的解决方案。
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调用才能得到所有,而我的解决方案则是在一个调用中获得所有产品。
发布于 2017-05-15 15:02:27
查询返回对象列表,因为在查询主节点时不添加任何筛选器。这和说的“一样”
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定义为节点的密钥。
移动结构到原来的岗位..。
https://stackoverflow.com/questions/43981287
复制相似问题