【问题标题】:Formatting a JSONArray in Java在 Java 中格式化 JSONArray
【发布时间】:2018-08-01 12:50:22
【问题描述】:

所以我正在构建一个食谱应用程序。我现在想在 TextView 对象的列表中显示成分。

下面的代码是我目前的做法。成分(在成分线中)只是放入一个字符串中。

JSONObject obj = new JSONObject(response);
JSONArray hits = obj.getJSONArray("hits");
for (int i = 0; i < hits.length(); i++) {
    JSONObject a = hits.getJSONObject(i);
    JSONObject recipe = a.getJSONObject("recipe");
    ListItem item = new ListItem(
        recipe.getString("label"),
        recipe.getString("source"),
        recipe.getString("image"),
        recipe.getString("ingredientLines"),
        recipe.getString("url")
    );
}

这就是字符串现在的样子

"ingredientLines" : [ "1 pound multigrain spaghetti (recommended: Barilla Plus)", "Kosher salt", "2 teaspoons black peppercorns", "3 tablespoons unsalted butter", "1 cup coarsely grated Pecorino Romano cheese", "2 full handfuls baby arugula, roughly chopped" ]

我希望它看起来像

"1 pound multigrain spaghetti (recommended: Barilla Plus)\n Kosher..."

这是我正在解析的 JSON

"recipe" : {
    "uri" : "http://www.edamam.com/ontologies/edamam.owl#recipe_7a38f039e2a9d3df25e65cc64bc0f87d",
    "label" : "The Secret Ingredient (Black Pepper): Multigrain Cacio e Pepe with Arugula Recipe",
    "image" : "https://www.edamam.com/web-img/f59/f59ec1a536535e9072bbf9a7c2432779.jpg",
    "source" : "Serious Eats",
    "url" : "http://www.seriouseats.com/recipes/2011/02/the-secret-ingredient-black-pepper-multigrain-cacio-e-pepe.html",
    "shareAs" : "http://www.edamam.com/recipe/the-secret-ingredient-black-pepper-multigrain-cacio-e-pepe-with-arugula-recipe-7a38f039e2a9d3df25e65cc64bc0f87d/pep",
    "yield" : 4.0,
    "dietLabels" : [ "Balanced" ],
    "healthLabels" : [ "Sugar-Conscious", "Vegetarian", "Peanut-Free", "Tree-Nut-Free", "Alcohol-Free" ],
    "cautions" : [ ],
    "tags" : [ "pasta", "pepper", "The Secret Ingredient", "vegetarian" ],
    "ingredientLines" : [ "1 pound multigrain spaghetti (recommended: Barilla Plus)", "Kosher salt", "2 teaspoons black peppercorns", "3 tablespoons unsalted butter", "1 cup coarsely grated Pecorino Romano cheese", "2 full handfuls baby arugula, roughly chopped" ]
}

【问题讨论】:

  • 你是如何设置你的 textview 值的? (请提供代码)
  • @RafaelPaulino 它被放在回收站视图中。我添加了代码,虽然我可以设置我只是想知道如何格式化它的文本。
  • 你能用你得到的整个响应 json 来扩展你的 json 示例,而不仅仅是食谱部分吗?

标签: java json string parsing gson


【解决方案1】:

使用String.join("\n", ingredientLines) 将数组转换为字符串,项目之间使用“\n”。

所以,不要使用recipe.getString("ingredientLines")

String.join("\n", recipe.getJSONArray("ingredientLines"))

【讨论】:

  • Android Studio 正在寻找更高的最低 API 来使用该调用。你能想到任何替代方案吗?该 API 为 26,这意味着我必须将我的应用程序限制为 Android 8.0 及更高版本,这让我感到不舒服。
【解决方案2】:
  ingredients = ingredients.replace("[", "");
  ingredients = ingredients.replace("]", "");
  ingredients = ingredients.replace("\"", "");
  ingredients = ingredients.replace(",", "\n");

这行得通...

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 2018-05-22
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多