java - (Android)How to get data from JSON to show on listview -
this json data
{ "success": "1", "results": [ { "type": "1.popmusic", "posts": [ { "music": "1.aaa" }, { "music": "2.bbb" } ] } ] }
i need click kind of music such click "popmusic" on listview , then,intent new activity show music in popmusic ,from json have show "1.aaa" , "2.bbb" on listview.
i can't "posts" json.how can it.
this blog1 class
public class blog1 { string success; list<post> results; public list<post> getresults() { return results; } }
this post1 class
public class post1 { string name; public string getname() { return name; } public string getauthor() { return author; } // getter , setter
this 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); }
you can use in build android json library
refer how json array values in android?
refer android json array nested in array
first need results
jsonarray json2 = json.getjsonarray("results");
then loop inside ( assuming 0 first element )
jsonobject json3 = json2.getjsonobject(0);
then need posts
jsonarray json4 = json3.getjsonarray("posts");
then gain can loop inside json4
--- edit ---
i see using gson 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 main code
blog blog = gson.fromjson(jsonstring, blog.class); //i assuming 0th item both blog.getresults().get(0).getposts().get(0).getmusic();
if constructing custom adapter then, assuming going show posts 1 blog entry can posts as
list<post> posts = blog.getresults().get(0).getposts()
and pass posts custom adapter.
if have 2 adapters blog.getresults() first adapter clicking on item of adapter results.get(itemindex).getposts() second adapter
Comments
Post a Comment