2

I am getting JSON data. How to use JSONArray when download & display image in GridView. How can I do acheive this? I refer Download and Display Image in Android GridView. It is JSONObject.

here is my code.

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

here is refer code,

{
  "status": "ok",
  "count": 45,
  "count_total": 397,
  "pages": 9,
  "posts": [
    {
      "id": 12378,
      "url": "http://stacktips.com/tutorials/android/speech-to-text-in-android",
    },
    {...},
    {...}
  ]
}

here is my JSON,

[
  {
    "id": "95",
    "name": "Cherry",
    "menu": [
      {
        "menu_id": "18",
        "img": "925dd3ad6d50fa5686a82af26515be5d.jpg"
      }
    ]
  },
  {...},
  {...}
]
2
  • oh, why you have to use JSONArray? you don't map your json string to java object? Commented Feb 21, 2017 at 9:00
  • How to solve that? Commented Feb 21, 2017 at 9:04

3 Answers 3

2

try this code :

For refer code:

try{
    JSONObject  object = new JSONObject (result);
    JSONArray jsonArray = object.getJSONArray("posts");
    for(int i=0;i<jsonArray.length();i++){
         JSONObject object = jsonArray.getJSONObject(i);
         String id = object.getString("id");
         String img_url = object.getString("url");

         item = new GridItem();
          if (img_url!= null)
                   item.setImage(img_url);
            mGridData.add(item);
        }
     }
     catch (JSONException e){
           e.printStackTrace();
    }

For second JSON

 try{
    JSONArray jsonArray = new JSONArray(result);
    for(int i=0;i<jsonArray.length();i++){
         JSONObject object = jsonArray.getJSONObject(i);
         String id = object.getString("id");
         String name = object.getString("name");

         JSONArray menu = object.getJSONArray("menu");
         for(int j=0;j<menu.length();j++){
             JSONObject menu_object = menu.getJSONObject(j);
             String menu_id = menu_object.getString("menu_id");
             String image_url = menu_object.getString("img");
             item = new GridItem();
              if (image_url!= null)
                       item.setImage(image_url);
                mGridData.add(item);
            }
        }
     }
     catch (JSONException e){
           e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

6 Comments

img.getString("url") I change image_url but can't solve method getString
check the edit...and there is two JSON ? which JSON image url u want to parse?
org.json.JSONException: Index 0 out of range [0..0) image_url is null
I use For second JSON; i add GridItem item; before for loop.
Log.i("hello",image_url); hello: 925dd3ad6d50fa5686a82af26515be5d.jpg but it doesn't show in GridView. I think something wrong me another code . Thank you
|
1

Here is your solution :

    try {
        JSONObject response = new JSONObject(result);

        JSONArray menu = response.optJSONArray("menu");

        GridItem item;

        for (int i = 0; i < menu.length(); i++) {

            JSONObject post = menu.optJSONObject(i);

            String menu_id=post.getString("menu_id");

            String img=post.getString("img");


            item = new GridItem();

            item.setImage(img);


            mGridData.add(item);

        }
    } 
}

Comments

0

Please use google-gson library link

Example JSON String

{
  "title": "Free Music Archive - Albums",
  "message": "",
  "errors": [],
  "total": "11259",
  "total_pages": 2252,
  "page": 1,
  "limit": "5",
  "dataset": [
    {
      "album_id": "7596",
      "album_title": "Album 1",
      "images": [
        {
          "image_id": "1",
          "user_id": null
        }
      ]
    }
  ]
}

Albums.java

class Albums {
    public String title;
    public String message;
    public List<string> errors = new ArrayList<string>();
    public String total;
    public int total_pages;
    public int page;
    public String limit;
    List<dataset> dataset = new ArrayList<dataset>();
}

Main Class

Albums albums = gson.fromJson(jsonString, Albums.class);

Reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.