2015-11-07 74 views
1

在我的程序中,我能够获取我的所有文本。但是在JSON数组中的JSOn中,有另一个具有图像URL的JSONArray。我正在使用JSON数组中的所有网址,但当我使用时,我没有收到任何回应。如何将我的ImageView替换为存储在URL中的URL。我的代码是在这里如何获取JSONArray值

private void parseResult(String result) { 
    try { 
     JSONObject response = new JSONObject(result); 
     JSONArray posts = response.optJSONArray("items"); 
     GridItem item; 
     for (int i = 0; i < posts.length(); i++) { 
      JSONObject post = posts.optJSONObject(i); 
      String title = post.optString("itemname"); 
      item = new GridItem(); 
      item.setTitle(title); 
      JSONArray attachments = post.getJSONArray("itemimage"); 
      if (null != attachments && attachments.length() > 0) { 
       JSONObject attachment = attachments.getJSONObject(0); 
       if (attachment != null) 
        item.setImage(attachment.getString("")); 
       } 
       mGridData.add(item); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

请发布您的logcat。 – Pankaj

+0

显示'itemimage' JSONArray从服务器得到的字符串 –

+0

HI @ρяσѕρєяK我正在获取我所有的项目图像url,但完成循环后,显示的东西 –

回答

0

恩,我2分钟后发布出来... cuz为你写演示。

您需要的解析方法在下方,您可以使用通用图像加载程序之类的框架将url加载到imageView。

private String parseaaa(String jsonString) { 

    String imgUrl = ""; 
    try { 

     JSONObject mJsonObject = new JSONObject(jsonString); 
     JSONArray mArray = mJsonObject.getJSONArray("items"); 
     for (int i = 0; i < mArray.length(); i++) { 

      JSONObject object=mArray.getJSONObject(i); 
      imgUrl+=object.getString("itemimage")+"\n"; 
      Log.i(TAG, imgUrl); 

     } 

    } catch (Exception e) { 
     // TODO: handle exception 
    } 

    return imgUrl; 
} 

enter image description here

顺便说一句,只是用JSONArray[]JSONObject之间进行负载的东西加载obj的...这是很容易做到。

+0

Thnxx兄弟...其实我是新的JSON领域,所以我很困惑如何从JSON数组中获得价值.. thnx @NilDroid ..非常muchh –

0

,我可以看到你的JSON你应该

JSONObject attachment = attachments.getJSONObject(0); 

得到JSONException,因为你正在创造对象中不存在那里,所以我建议简单地从你的JSONArray提取String,并做你的setImage的东西..

if (null != attachments && attachments.length() > 0) { 
     String image= (String) attachments.get(0); 
     //--- set your image here--// 
} 
+0

嗨mohit .. thnxxx bro ..你是生命的救星.. Thnxx为你的快速反应,但我只能得到一个图像... rst前两个图像不出现在网格 –

+0

你正在得到所有的网址,对不对? – Mohit

+0

嗨,我得到了一切.. thnxx兄 –