簡體   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