我可以在我的应用程序中记录 JSON 文件,但我在解析 JSON 文件时遇到问题。我试图获取每个食谱的名称,以便在 RecyclerView 中显示它。稍后我确实需要来自 JSON 的更多数据,一次或根据需要进行所有解析会更好吗?
这是 JSON 的链接 https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json
JSONObject recipeObject = new JSONObject(jsonString);
JSONArray recipeResults = recipeObject.getJSONArray("name");
Log.d(LOGTAG, "Recipe Results " + recipeResults.toString());
这是日志结果
2018-12-13 13:56:43.622 31472-31472/com.shawn.nichol.bakingapp W/System.err: at com.bakingapp.Data.ExtractRecipeData.recipeData(ExtractRecipeData.java:19)
最佳答案
基础对象是一个对象数组。
试试这个:
JSONArray recipes = new JSONArray(jsonString);
for(int i = 0; i < recipes.length(); i++){
JSONObject recipe = recipes.getJSONObject(i);
Log.d(LOGTAG, "Recipe name: " + recipe.getString("name"));
}
关于android - JSON 解析帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53770647/