从Firebase获取对象(Json) [英] Getting objects out from Firebase(Json)

查看:219
本文介绍了从Firebase获取对象(Json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是firebase和Json的新手,所以有一些基本的麻烦。 Firebase中的我的Json文件是这样构建的:

  {
workout:{
Heavy胸部和手臂:卧推倾斜哑铃按有线交叉二头肌杠铃卷发交替哑铃卷发传道者卷发V型三头肌延伸头骨粉碎机小腿背和肩背:拉背,下巴,索排,侧向下拉,哑铃排,反向苍蝇, Arnold Press,Side Raises,Front Raises,Rotary Cuffs]
}
}



首先,我想列出所有锻炼列表,列表将包含重型胸部和手臂,光背和肩膀。之后,我想摆脱所有的元素孩子?)在这些名单,例如卧推,倾斜Dumbell按等我试了好几个小时,现在试试这个:

  myFirebaseRef.addValueEventListener(new ValueEventListener(){
@Overrid e
public void onDataChange(DataSnapshot dataSnapshot){


List< String> messages = dataSnapshot.getValue();



$ b @Override
public void onCancelled(FirebaseError firebaseError){

}
}) ;


解决方案

>

 锻炼
workout_id_0
类别:重型胸甲和武器
元素
element_id_0: true
element_id_1:true
workout_id_1
类别:腰背
元素
element_id_0:true

元素
element_id_0
名称:卧推
描述:平躺在背上,手臂在90度等
element_id_1
名称:倾斜哑铃按
说明:将背板倾斜30度等等

你可以用这个做一些动作结构:



假设您想在重型胸罩和轻型背部锻炼中加入卧推。这种结构使您可以重新使用您的元素。



一年之后,您想将Bench press的名称更改为Bench Press SUPER DUPER。您只需更改名称:在element_id_0之内并且就位



您可以轻松查询哪些训练包括element_id_1,斜线哑铃按

您可以轻松更改锻炼的名称。而不是轻微的背部和肩膀,它可能是轻的背部和肩部,只是改变一个孩子的节点。

你应该进一步考虑你想如何使用你的数据;你需要找到所有的元素,只是为了腿,并创建一个基于这些的锻炼?你需要查询或添加额外的数据?



打印出所有锻炼类别名称:



<$ p $($code> ref.on(child_added,function(snapshot,prevChildKey){
var workout = snapshot.val();
console.log(Category:+ workout.category);
//元素引用也在这里!elements = workout.elements
});

获取每个锻炼的元素名称需要一点点代码,但是这会让您走。

I am new to firebase and Json so having some basic troubles with it. My Json file in Firebase is built like this:

{
  "workout" : {
    "Heavy Chest and Arms" : [ "Bench press", "Incline Dumbbell Press", "Cable Crossover", "Bicep Barbell Curls", "Alternate Dumbbell Curls", "Preachers Curls", "V-Bar Triceps Extensions", "Skull Crushers", "Sitting Calf raises" ],
    "Light Back and Shoulders" : [ "Pull Ups", "Chins", "Cable Rows", "Lateral Pulldowns", "Dumbbell Rows", "Reverse Flies", "Arnold Press", "Side Raises", "Front Raises", "Rotary Cuffs" ]
  }
}

First I want to get out a list with all the workouts, that list would contain Heavy Chest and Arms","Light Back and shoulders". After that I want to get out all the elements(child?) in those list, example "Bench press","Incline Dumbell press" etc. I tried several times for hours now experimenting with this:

myFirebaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {


                List<String> messages = dataSnapshot.getValue();


            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

解决方案

Here's a better structure to get you going

workouts
  workout_id_0
     category: "Heavy Chest and Arms"
     elements
        element_id_0: true
        element_id_1: true
  workout_id_1
     category: "Light Back and Shoulders"
     elements
        element_id_0: true

elements
  element_id_0
    name: "Bench press"
    description: "While laying flat on your back, arms at 90 degrees etc"
  element_id_1
    name: "Incline Dumbbell Press"
    description: "Incline the backboard 30 degrees and etc etc"

You can do some snappy things with this structure:

Say you want to include a bench press in both the Heavy Chest and Light back workouts. This structure enables you to re-use your elements.

A year from now, you want to change the name of Bench press to Bench Press SUPER DUPER. You just change the name: within the element_id_0 and that falls into place

You can easily query for which workouts include element_id_1, the Incline dumbbell press

You can easily change the name of your workout. Instead of Light Back and Shoulders, it could be Light Lower Back and Shoulders with just changing one child node.

You should further consider how you want to use your data; do you need to find all elements that are just for the legs and create a workout based on those? Do you need to query or add additional data?

To print out all of the workout category names:

ref.on("child_added", function(snapshot, prevChildKey) {
  var workout = snapshot.val();
  console.log("Category: " + workout.category);
  //the element references are here too! elements = workout.elements
});

It will take a little code to get the element names for each workout but this should get you going.

这篇关于从Firebase获取对象(Json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆