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);


}
1
  • Can u post the class used to fetch/store the JSON data? Commented Jul 14, 2015 at 4:09

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

How can I use getPosts() on function main?
I have added the main code, It is an array of objects which contains array of objects, which should accessed using index of index
I think it useful but I don't know what type of variable to use for it such as List<Post> results = blog.getResults().get(0).getPosts().get(0).getName(); can you suggest me for that.Because I need to send to customadapter.
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
Ohhh,Now I can get it.Thank you very much
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.

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.