繁体   English   中英

如何访问 JSON 元素?

[英]How to access the JSON element?

 @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@

  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
}
// 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")

希望能帮助到你!

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}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM