0

This is my JSON data

{
    "success": "1",
    "results": [
        {
            "type": "1.popmusic",
            "posts": [
                {
                    "music": "1.AAA"
                },
                {
                    "music": "2.BBB"
                }
            ]
        }
    ]
}

I need to click some kind of music such as I click "popmusic" on my listview and then,Intent to new activity to show music in popmusic ,From my json It have to show "1.AAA" and "2.BBB" on listview.

I can't get "posts" from my json.How can I get it.

This is Blog1 Class

public class Blog1 {
    String success;
    List<Post> results;
    public List<Post> getResults() {
        return results;
    }

}

This is Post1 class

public class Post1 {
String name;
public String getName() {
    return name;
}

public String getAuthor() {
    return author;
}

// Getter and Setter

This is main class

  private void showData(String jsonString) {
        Gson gson = new Gson();
        Blog1 blog = gson.fromJson(jsonString, Blog1.class);
        List<Post> results = blog.getResults();

        mAdapter = new CustomAdapter1(this, results);
        mListView.setAdapter(mAdapter);


}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Adiruj
  • 23
  • 6

2 Answers2

0

You can use the in build android JSON library

Refer How to get json array values in android?

Refer android get json array nested in array

First you need to get results

JSONArray json2 = json.getJSONArray("results");

Then loop inside ( I am assuming 0 for first element )

JSONObject json3 = json2.getJSONObject(0);

Then you need to get posts

JSONArray json4 = json3.getJSONArray("posts");

Then gain you can loop inside json4

--- EDIT ---

I see that you are using Gson but your class structure should be

public class Blog {
    String success;
    List<Result> results;
    public List<Result> getResults() {
        return results;
    }
}

public class Result {
   String type;
   public String getType() {
       return type;
   }
   List<Post> posts;
   public List<Post> getPosts() {
        return posts;
   }
}

public class Post {
   String music;
   public String getMusic() {
       return music;
   }
}

In your main code

Blog blog = gson.fromJson(jsonString, Blog.class);
//I am assuming 0th item for both
blog.getResults().get(0).getPosts().get(0).getMusic();

If you are constructing a custom adapter then, assuming that you are going to show all posts for one blog entry then you can get all posts as

List<Post> posts = blog.getResults().get(0).getPosts()

And pass the posts to your custom adapter.

If you have two adapters then blog.getResults() for first adapter then clicking on an item of that adapter results.get(itemindex).getPosts() for the second adapter

Community
  • 1
  • 1
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
  • How can I use getPosts() on function main? – Adiruj Jul 14 '15 at 07:15
  • I have added the main code, It is an array of objects which contains array of objects, which should accessed using index of index – Dickens A S Jul 14 '15 at 09:30
  • I think it useful but I don't know what type of variable to use for it such as List results = blog.getResults().get(0).getPosts().get(0).getName(); can you suggest me for that.Because I need to send to customadapter. – Adiruj Jul 14 '15 at 10:20
  • If you want to send all the posts to an adapter simply put blog.getResults().get(0).getPosts()...... assuming you are going to show all posts for one blog entry – Dickens A S Jul 14 '15 at 11:22
  • Ohhh,Now I can get it.Thank you very much – Adiruj Jul 15 '15 at 02:25
0

Your JSON

{
    "success": "1",
    "results": [
        {
            "type": "1.popmusic",
            "posts": [
                {
                    "music": "1.AAA"
                },
                {
                    "music": "2.BBB"
                }
            ]
        }
    ]
}

Is actually mapped to

public class Response {
    private String success;
    private List<Result> results;

    //getter, setter
}

public class Result {
    private String type;
    private List<Post> posts;

    //getter, setter
}

public class Post {
    private String music;

    //getter, setter
}

Then you can make GSON get a Result out of it.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428