首页 > 解决方案 > JSON 反序列化 - Firebase 到 Android 应用程序

问题描述

我目前正在开发一个 android 应用程序,我的 firebase 已链接,我目前陷入 JSON 序列化(JSON - Java 数据转换)。我创建了一个名为“cocktail”的 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

}

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);

    }

鸡尾酒 JSON 格式

标签: androidjsonfirebasefirebase-realtime-database

解决方案


推荐阅读