JSON反序列化-Firebase到Android应用程序

JSON反序列化-Firebase到Android应用程序,android,json,firebase,firebase-realtime-database,Android,Json,Firebase,Firebase Realtime Database,我目前正在开发一个android应用程序,我的firebase是链接的,我目前正在进行JSON序列化(JSON-Java数据转换)。我创建了一个名为“鸡尾酒”的java类,并将数据存储在firebase中,如下所示。 我有很多问题:我是否需要让java数据类型的名称与数据库中的类型相同?我可以在JAVA中使用属性名称设置器吗?要读取和传输此数据,我会创建一个鸡尾酒对象并设置值,还是使用哈希映射在本地存储数据 我目前正在尝试设计我的JSON解析器,不确定它是否正确,或者是否可以提高效率,非常感谢您

我目前正在开发一个android应用程序,我的firebase是链接的,我目前正在进行JSON序列化(JSON-Java数据转换)。我创建了一个名为“鸡尾酒”的java类,并将数据存储在firebase中,如下所示。 我有很多问题:我是否需要让java数据类型的名称与数据库中的类型相同?我可以在JAVA中使用属性名称设置器吗?要读取和传输此数据,我会创建一个鸡尾酒对象并设置值,还是使用哈希映射在本地存储数据

我目前正在尝试设计我的JSON解析器,不确定它是否正确,或者是否可以提高效率,非常感谢您的帮助

public class cocktail {
private String name;
private String glass;
private JSONArray ingredients; //this is now a json array
private String[] recipe;
private String description;
private JSONArray garnish; //this is a json array aswell
private Boolean alcoholic;
//private Float Abv;
private String[] barware;
private String[] tags;
private String image;
private String video;

public cocktail() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

// constructor
public cocktail(String name, String glass, JSONArray ingredients, String[] recipe, String description, JSONArray garnish, Boolean alcoholic, String[] barware, String[] tags, String image, String video) {
    this.name = name;
    this.glass = glass;
    this.ingredients = ingredients;
    this.recipe = recipe;
    this.description = description;
    this.garnish = garnish;
    this.alcoholic = alcoholic;
    //this.Abv = Abv;
    this.barware = barware;
    this.tags = tags;
    this.image = image;
    this.video = video;
}


//-------------
//Getter methods

//Get method for grabbing the name of "this" cocktail
public String getName() {
    return name;
}

//Get method for grabbing the glass of "this" cocktail
public String getGlass() {
    return glass;
}

//Get method for grabbing the ingredients list of "this" cocktail
public JSONArray getIngredients() {
    return ingredients;
    // return root.getJSONArray("ingredients");
}

//Get method for grabbing an ingredient from the ingredients list of "this" cocktail //change
public JSONObject getIngredient(JSONArray ingredientsList, String ingredientName) {
    try {
        JSONArray ingredientsArray = ingredientsList;
        for(int i=0;i<ingredientsArray.length();i++){

            JSONObject theIngredient = ingredientsArray.getJSONObject(i);
            JSONObject getThisIngredient = new JSONObject();
            //String ingredientValue = theIngredient.optString("ingredient");
            String matchIngredient = getThisIngredient.getString("ingredient");
            if( matchIngredient == ingredientName) {

                Log.d("ingredient","this ingredient: ");
                return getThisIngredient;

                //return the object if the string matches
            }
            //getThisIngredient = ingredients.getJSONObject(ingredientName);
            //JSONReader reader = new JSONreader(new InputStreamReader(ingredients, JSONArray));
            //System.out.print(ingredientValue);
            //Log.d(ingredientValue, "Whats this value?");

        }
    }
    catch (JSONException e){
        e.printStackTrace();
    }


    return null;
}


//Get method for grabbing the recipe of "this" cocktail
public String[] getRecipe() {
    return recipe;
}

//is this needed?
//Get method for grabbing a single line of this cocktail recipe of "this" cocktail
public String getSingleLineOfRecipe(Integer i) {
    return recipe[i];
}

//Get method for grabbing the description of "this" cocktail
public String getDescription() {
    return description;
}

//Get method for grabbing the garnish(s) of "this" cocktail
public JSONArray getGarnish() {
    return garnish;
}

//Get method for grabbing the alcoholic-boolean of "this" cocktail
public Boolean getAlcoholic() {
    return alcoholic;
}

/*
//Get method for grabbing the Alcohol By Volume of "this" cocktail
private Float getAbv() {
    return Abv;
}
*/

//Get method for grabbing the barware list  of "this" cocktail
public String[] getBarware() {
    return barware;
}

//Get method for grabbing the tags of "this" cocktail
public String[] getTags() {
    return tags;
}

//Get method for grabbing a tab of index i of "this" cocktail
public String getTag(Integer i) {
    return tags[i];
}

//Get method for grabbing an image of "this" cocktail
public String getImage() {
    return image;
}

//Get method for grabbing a video of "this" cocktail
public String getVideo() {
    return video;
}


//--------------
//Setter methods

//Method for setting the name of "this" cocktail
public void setName(String name) {
    this.name = name;
}

//Method for setting the glass of "this" cocktail
public void setGlass(String glass) {
    this.glass = glass;
}

//Method for setting the ingredients of "this" cocktail
public void setIngredients(JSONArray ingredients) {
    this.ingredients = ingredients;
}

//Method for setting an ingredient of index i of "this" cocktail
public void setIngredient(Integer amount, String ingredient, String measurement, Integer i) throws JSONException {
    JSONObject newIngredient = new JSONObject();
    try {
        newIngredient.put("amount", amount);
        newIngredient.put("ingredient", ingredient);
        newIngredient.put("measurement", measurement);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    finally {
        ingredients.put(i, newIngredient);
    }
}


//Method for setting the recipe of "this" cocktail
public void setRecipe(String[] recipe) {
    this.recipe = recipe;
}

//change?
//Method for setting a single line of the recipe at index i of "this" cocktail
public void setSingleLineOfRecipe(String newRecipeLine, Integer i) {
    this.recipe[i] = newRecipeLine;
}

//Method for setting the description of "this" cocktail
public void setDescripton(String description) {
    this.description = description;
}

//Method for setting the garnishes of "this" cocktail
public void setGarnishes(JSONArray garnish) {
    this.garnish = garnish;
}

//Method for setting a single garnish at index i of "this" cocktail
public void setGarnish(JSONArray garnish) {
    this.garnish = garnish;
}

//Method for setting the alcholic-boolean of "this" cocktail
public void setAlcoholic(Boolean alcoholic) {
    this.alcoholic = alcoholic;
}

/*
//Method for setting the Alcohol By Volume of "this" cocktail
private void setAbv(Float Abv) {
    this.Abv = Abv;
}
*/

//Method for setting the barware of "this" cocktail
public void setBarware(String[] barware) {
    this.barware = barware;
}

//change?
//Method for setting a specific piece of barware of "this" cocktail
public void setSingleItemOfBarware(String newBarware, Integer i) {
    this.barware[i] = newBarware;
}

//Method for setting the tags of "this" cocktail
public void setTags(String[] tags) {
    this.tags = tags;
}

//Method for setting a tag of index i of "this" cocktail
public void setTag(String tag, Integer i) {
    this.tags[i] = tag;
}

//Method for setting an image of "this" cocktail
public void setImage(String url) {
    this.image = url;
}

//Method for setting a video of "this" cocktail
public void setVideo(String url) {
    this.video = url;
}
// other methods
公共类鸡尾酒{
私有字符串名称;
私人弦玻璃;
私有JSONArray组件;//这现在是一个json数组
私有字符串[]配方;
私有字符串描述;
private JSONArray garnish;//这也是一个json数组
私家侦探;
//私人浮动Abv;
私人字符串[]酒吧;
私有字符串[]标记;
私有字符串图像;
私有字符串视频;
公共鸡尾酒会{
//调用DataSnapshot.getValue(User.class)所需的默认构造函数
}
//建造师
公共鸡尾酒(字符串名称、字符串玻璃杯、JSONArray配料、字符串[]配方、字符串描述、JSONArray装饰、布尔酒精、字符串[]酒器、字符串[]标签、字符串图像、字符串视频){
this.name=名称;
这个玻璃=玻璃;
这个。成分=成分;
this.recipe=配方;
this.description=描述;
this.garnish=装饰;
这个。酒精的=酒精的;
//这个。Abv=Abv;
this.barware=barware;
this.tags=标签;
这个图像=图像;
这个视频=视频;
}
//-------------
//吸气剂法
//获取获取“此”鸡尾酒名称的方法
公共字符串getName(){
返回名称;
}
//获取抓取“此”鸡尾酒杯的方法
公共字符串getGlass(){
返回玻璃;
}
//获取获取“此”鸡尾酒成分列表的方法
公共JSONArray getComponents(){
返回成分;
//返回root.getJSONArray(“配料”);
}
//获取从“此”鸡尾酒的配料列表中获取配料的方法//更改
公共JSONObject GetComponent(JSONArray ingredientsList,String ingredientName){
试一试{
JSONArray ingredientsArray=ingredientsList;

对于(int i=0;ii如果您感兴趣,就是如何使用
FirestoreRecyclerAdapter
从Cloud Firestore数据库检索数据并在
RecyclerView
中显示它。如果您使用java命名约定,您的代码将更容易理解:类名大写,而变量和方法则是编写的小写。谢谢,我的拙劣做法将有所改变。我将查看firestoreRecycler适配器,但我目前正在使用firebase。
private void showData(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        cocktail cocktailEntry = new cocktail();
        //Setting the cocktail name
        cocktailEntry.setName(ds.child(userID).getValue(cocktail.class).getName());

        //Setting the cocktail glass
        cocktailEntry.setGlass(ds.child(userID).getValue(cocktail.class).getGlass());

        //Setting the cocktail ingredients
        cocktailEntry.setIngredients(ds.child(userID).getValue(cocktail.class).getIngredients());

        //Setting the cocktail recipe
        cocktailEntry.setRecipe(ds.child(userID).getValue(cocktail.class).getRecipe());

        //Setting the cocktail description
        cocktailEntry.setDescripton(ds.child(userID).getValue(cocktail.class).getDescription());

        //Setting the cocktail garnish
        cocktailEntry.setGarnish(ds.child(userID).getValue(cocktail.class).getGarnish());

        //Setting the cocktail slcoholic
        cocktailEntry.setAlcoholic(ds.child(userID).getValue(cocktail.class).getAlcoholic());

        //Setting the cocktail barware
        cocktailEntry.setBarware(ds.child(userID).getValue(cocktail.class).getBarware());

        //Setting the cocktail tags
        cocktailEntry.setTags(ds.child(userID).getValue(cocktail.class).getTags());

        //Setting the cocktail image link - Google Storage
        cocktailEntry.setImage(ds.child(userID).getValue(cocktail.class).getImage());

        //Setting the cocktail video link for youtube embedding
        cocktailEntry.setVideo(ds.child(userID).getValue(cocktail.class).getVideo());

        //Display all the data
        Log.d("Cocktail List download", "showData: Name" + cocktailEntry.getName());
        Log.d("Cocktail List download", "showData: Glass" + cocktailEntry.getGlass());
        Log.d("Cocktail List download", "showData: Ingredients" + cocktailEntry.getIngredients());
        Log.d("Cocktail List download", "showData: Recipe" + cocktailEntry.getRecipe());
        Log.d("Cocktail List download", "showData: Description" + cocktailEntry.getDescription());
        Log.d("Cocktail List download", "showData: Garnish" + cocktailEntry.getGarnish());
        Log.d("Cocktail List download", "showData: Alcoholic" + cocktailEntry.getAlcoholic());
        Log.d("Cocktail List download", "showData: Barware" + cocktailEntry.getBarware());
        Log.d("Cocktail List download", "showData: Tags" + cocktailEntry.getTags());
        Log.d("Cocktail List download", "showData: Image" + cocktailEntry.getImage());
        Log.d("Cocktail List download", "showData: Video" + cocktailEntry.getVideo());

        ArrayList<cocktail> cocktailData  = new ArrayList<>();
        //Make a JSONObject of type cocktail and add this data to the object, then object to the list

        //am i creating a cocktail object or a JSONObject?
        cocktail newCocktail = new cocktail();
        newCocktail.addProperty("name", cocktailEntry.getName());
        newCocktail.addProperty("glass", cocktailEntry.getGlass());
        newCocktail.addProperty("glass", cocktailEntry.getGlass());
        newCocktail.addProperty("glass", cocktailEntry.getGlass());
        newCocktail.addProperty("glass", cocktailEntry.getGlass());
        newCocktail.addProperty("glass", cocktailEntry.getGlass());
        newCocktail.addProperty("glass", cocktailEntry.getGlass());



        /*
        cocktailData.add(cocktailEntry.getName());
        cocktailData.add(cocktailEntry.getGlass());
        cocktailData.add(cocktailEntry.getIngredients());
        cocktailData.add(cocktailEntry.getRecipe());
        cocktailData.add(cocktailEntry.getDescription());
        cocktailData.add(cocktailEntry.getGarnish());
        cocktailData.add(cocktailEntry.getAlcoholic());
        cocktailData.add(cocktailEntry.getBarware());
        cocktailData.add(cocktailEntry.getTags());
        cocktailData.add(cocktailEntry.getImage());
        cocktailData.add(cocktailEntry.getVideo());
        */


        //ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);

        RecyclerView myrv = (RecyclerView) myView.findViewById(R.id.theCocktailList);
        cocktailListAdapter myAdapter = new cocktailListAdapter(getContext(), cocktailData);
        myrv.setLayoutManager(new GridLayoutManager(getActivity(),3));
        myrv.setAdapter(myAdapter);

        //cardViewCocktails.setAdapter(adapter);

    }