(Android)如何从 JSON 获取数据以显示在列表视图中
(Android)How to get data from JSON to show on listview
这是我的JSON数据
{
"success": "1",
"results": [
{
"type": "1.popmusic",
"posts": [
{
"music": "1.AAA"
},
{
"music": "2.BBB"
}
]
}
]
}
我需要点击某种音乐,例如我在我的列表视图上点击 "popmusic",然后,意图新 activity 以在流行音乐中显示音乐,来自我的 json 它必须在列表视图中显示“1.AAA”和“2.BBB”。
我无法从我的 json.How 中获取 "posts" 我可以获取它吗?
这是Blog1 Class
public class Blog1 {
String success;
List<Post> results;
public List<Post> getResults() {
return results;
}
}
这是Post1 class
public class Post1 {
String name;
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
// Getter and Setter
这是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);
}
您可以使用内置 android JSON 库
参考How to get json array values in android?
参考android get json array nested in array
首先你需要得到结果
JSONArray json2 = json.getJSONArray("results");
然后在内部循环(我假设第一个元素为 0)
JSONObject json3 = json2.getJSONObject(0);
然后你需要获取帖子
JSONArray json4 = json3.getJSONArray("posts");
然后gain你可以在json4里面循环
--- 编辑 ---
我看到您正在使用 Gson,但您的 class 结构应该是
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;
}
}
在你的主代码中
Blog blog = gson.fromJson(jsonString, Blog.class);
//I am assuming 0th item for both
blog.getResults().get(0).getPosts().get(0).getMusic();
如果您正在构建一个自定义适配器,假设您要显示一个博客条目的所有帖子,那么您可以将所有帖子显示为
List<Post> posts = blog.getResults().get(0).getPosts()
并将 posts 传递给您的自定义适配器。
如果您有两个适配器,那么第一个适配器 blog.getResults() 然后单击该适配器的一个项目 results.get( itemindex).getPosts() 第二个适配器
你的JSON
{
"success": "1",
"results": [
{
"type": "1.popmusic",
"posts": [
{
"music": "1.AAA"
},
{
"music": "2.BBB"
}
]
}
]
}
实际映射到
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
}
然后你可以让GSON从中得到一个Result
。
这是我的JSON数据
{
"success": "1",
"results": [
{
"type": "1.popmusic",
"posts": [
{
"music": "1.AAA"
},
{
"music": "2.BBB"
}
]
}
]
}
我需要点击某种音乐,例如我在我的列表视图上点击 "popmusic",然后,意图新 activity 以在流行音乐中显示音乐,来自我的 json 它必须在列表视图中显示“1.AAA”和“2.BBB”。
我无法从我的 json.How 中获取 "posts" 我可以获取它吗?
这是Blog1 Class
public class Blog1 {
String success;
List<Post> results;
public List<Post> getResults() {
return results;
}
}
这是Post1 class
public class Post1 {
String name;
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
// Getter and Setter
这是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);
}
您可以使用内置 android JSON 库
参考How to get json array values in android?
参考android get json array nested in array
首先你需要得到结果
JSONArray json2 = json.getJSONArray("results");
然后在内部循环(我假设第一个元素为 0)
JSONObject json3 = json2.getJSONObject(0);
然后你需要获取帖子
JSONArray json4 = json3.getJSONArray("posts");
然后gain你可以在json4里面循环
--- 编辑 ---
我看到您正在使用 Gson,但您的 class 结构应该是
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;
}
}
在你的主代码中
Blog blog = gson.fromJson(jsonString, Blog.class);
//I am assuming 0th item for both
blog.getResults().get(0).getPosts().get(0).getMusic();
如果您正在构建一个自定义适配器,假设您要显示一个博客条目的所有帖子,那么您可以将所有帖子显示为
List<Post> posts = blog.getResults().get(0).getPosts()
并将 posts 传递给您的自定义适配器。
如果您有两个适配器,那么第一个适配器 blog.getResults() 然后单击该适配器的一个项目 results.get( itemindex).getPosts() 第二个适配器
你的JSON
{
"success": "1",
"results": [
{
"type": "1.popmusic",
"posts": [
{
"music": "1.AAA"
},
{
"music": "2.BBB"
}
]
}
]
}
实际映射到
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
}
然后你可以让GSON从中得到一个Result
。