【问题标题】:How to access the JSON element?如何访问 JSON 元素?
【发布时间】:2020-03-30 17:12:34
【问题描述】:
 @Override
    protected void onPostExecute(String response) {
        String firstName = null;
        String lastName = null;
        try {
            JSONObject jsonResponse = new JSONObject(response);
            JSONArray jsonArray = jsonResponse.getJSONArray("");
            JSONObject userInfo = jsonArray.getJSONObject(0);

            firstName = userInfo.getString("id");
            lastName = userInfo.getString("id");

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

我有这个 JSON 文件 https://api.myjson.com/bins/1a5t7w 我如何才能访问那个值,我使用了这段代码,但它不起作用? 它返回@null@

【问题讨论】:

  • 作为第一步,您必须将“响应”字符串传递给 JSONObject 构造函数
  • 哎呀,我忘了在代码中添加它

标签: java android arrays json api


【解决方案1】:
  1. 您在响应中得到一个 JSON 数组。
  2. 要解析此数组,您需要将响应传递给 JSONArray 对象,如下所示
try {
    JSONArray rspArr = new JSONArray(response);
    for(int i= 0; i< rspArr.length(); i++){
        JSONObject jsonObject = (JSONObject) rspArr.get(i);
        //Your logic
    }
}catch (Exception e ){
        //log exception
}

【讨论】:

    【解决方案2】:
    // since you know is an array, create a JSONArray based on your response String    
    JSONArray array = JSONArray(response)
    // obtain the first element of your array as a JSONObject
    JSONObject jsonObject = array.getJSONObject(0)
    // once you get the jsonObject you can start getting the values you need
    String id = jsonObject.getString("id")
    

    希望对你有帮助!

    【讨论】:

      【解决方案3】:

      Arunangshu 的回答似乎是正确的,您需要先提取 JSONArray,因为 API 返回的是 JSONArray(JSON 对象数组)而不是一个大的 JSON 对象。

      可以使用http://jsonviewer.stack.hu/ 查看 JSON。它可以更好地理解 JSON 结构。

      对于 Java,可以使用http://www.jsonschema2pojo.org/ 将 JSON 对象(不是数组)转换为 Java Pojo 类(仔细选择选项)。

      以下是 json 数组中的第一个对象 -

      {"id":48191,"title":"Apple Crumble Recipe","image":"https://spoonacular.com/recipeImages/48191-312x231.jpg","imageType":"jpg","usedIngredientCount":1,"missedIngredientCount":2,"missedIngredients":[{"id":4073,"amount":35.0,"unit":"g","unitLong":"grams","unitShort":"g","aisle":"Milk, Eggs, Other Dairy","name":"margarine","original":"35 g margarine or butter","originalString":"35 g margarine or butter","originalName":"margarine or butter","metaInformation":[],"meta":[],"image":"https://spoonacular.com/cdn/ingredients_100x100/butter-sliced.jpg"},{"id":8120,"amount":35.0,"unit":"g","unitLong":"grams","unitShort":"g","aisle":"Cereal","name":"rolled oats","original":"35 g rolled oats","originalString":"35 g rolled oats","originalName":"rolled oats","metaInformation":[],"meta":[],"image":"https://spoonacular.com/cdn/ingredients_100x100/rolled-oats.jpg"}],"usedIngredients":[{"id":9003,"amount":400.0,"unit":"g","unitLong":"grams","unitShort":"g","aisle":"Produce","name":"apples","original":"400 g cooking apples peeled cored and quartered","originalString":"400 g cooking apples peeled cored and quartered","originalName":"cooking apples peeled cored and quartered","metaInformation":["cored","peeled","quartered"],"meta":["cored","peeled","quartered"],"image":"https://spoonacular.com/cdn/ingredients_100x100/apple.jpg"}],"unusedIngredients":[],"likes":965}
      

      【讨论】:

      • 使用 API 是一个深刻而有趣的话题,我将很快详细研究
      猜你喜欢
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2018-04-01
      • 2016-11-01
      • 2017-08-12
      • 2012-09-09
      • 1970-01-01
      相关资源
      最近更新 更多