【问题标题】:Having trouble parsing json using volley使用 volley 解析 json 时遇到问题
【发布时间】:2019-10-26 21:07:46
【问题描述】:

我正在尝试使用 volley lib 从 flickr API 获取数据。

这是我的 json :

{
  "photos": {
    "page": 1,
    "pages": 48,
    "perpage": 100,
    "total": "4793",
    "photo": [
      {
        *"id": "48955365182",
        "owner": "49191827@N00",
        *"secret": "fd5e5fd91c",
        *"server": "65535",
       * "farm": 66,
        "title": "permitted burn. thermal, ca. 2019.",
        "ispublic": 1,
        "isfriend": 0,
        "isfamily": 0
      }, ...
   }
}

我正在尝试获取带有“ * ”的字段:


private void parseJson() {

    String URL = "https://www.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key=x&user_id=49191827%40N00&extras=&format=json&nojsoncallback=1";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    JSONArray jsonArray ;
                    try {
                        jsonArray = response.getJSONArray("photo");
                        for(int i=0; i<jsonArray.length(); i++){
                            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                            Log.d("GGG",jsonObject.getString("secret"));
                            Log.d("GGG",jsonObject.getString("id"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.d("VVV",e.getMessage());
                    }


                }
            }, new Response.ErrorListener() {

            }
        }
    );
}

【问题讨论】:

    标签: java android json android-volley


    【解决方案1】:

    你需要从最顶层的对象开始解析

    jsonArray = response.getJSONObject("photos").getJSONArray("photo")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      相关资源
      最近更新 更多