В настоящее время я работаю над приложением для Android, моя база данных связана, и я в настоящее время застрял в сериализации 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
}
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);
}