如何到达图像节点



我具有下面的JSON结构,我无法触及图像节点。我该如何使用凌图书馆做到这一点?任何想法都会有所帮助。预先感谢。

{
  "nodes": [
    {
      "node": {
        "id": "2536",
        "title": "Die Düsseldorfer Rabbiner",
        "image": {
          "src": "how to reach here",
          "alt": "",
          "title": "© Landeshauptstadt Düsseldorf/Michael Gstettenbauer"
        },
        "body": "some text",
        "category": "Deutsch",
        "created": "1481344134",
        "url": "some text"
      }
    }
  ]
}

代码块

JsonArrayRequest getRequest = new JsonArrayRequest(
            url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    try {
                        JSONObject jsonObject = new JSONObject("response");
                        JSONArray jsonArray = jsonObject.getJSONArray("nodes");
                        for(int i = 0; i<jsonArray.length(); i++){
                            JSONObject jo = jsonArray .getJSONObject(i);
                            String name = jo.getString("category");
                            Toast.makeText(context, name, Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
From the look of things you should  use JsonObjectRequest instead  JsonArrayRequest.
 JsonObjectRequest request=new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray  array = response.getJSONArray("nodes");
                        for(int i=0;i<array.length();i++){
                            JSONObject object= array.getJSONObject(i);
                            JSONObject imageObject= object.getJSONObject("node").getJSONObject("image");
                            Toast.makeText(TestActivity.this, imageObject.toString(), Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });

您的Java模型应与JSON 完全匹配。JSON模式中的基本元素是对象,而不是数组。因此,您的请求应该是JSONObjectRequest,而不是JSONArrayRequest

JSONObjectRequest request = new JSONObjectRequest(url,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONArray response) {
                    try {
                        JSONArray nodes = response.getJSONArray("nodes");
                        for(int i = 0; i < nodes.length; i++) {
                            JSONObject obj = nodes.getJSONObject(i);
                            JSONObject node = obj.getJSONObject("node");
                            JSONObject image = node.getJSONObject("image");
                            String title = image.getString("title");
                            Toast.makeText(context, title, Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

您可以看到,如果您有一个大数据集,这可能会变得乏味。这就是为什么我强烈建议使用JSON解析库的原因;例如Gson,Moshi或Jackson。有一个很好的教程,关于如何与凌空作出自定义请求以在培训文档中使用这些图书馆。

最新更新