我有这种类型的JSON数据,我有一个问题,解析列表的数据!
- 这是我的JSON数据**
[
{
"id": 17502,
"link": "https://www.angrybirds.com/blog/get-ready-angry-birds-movie-2-premiere-new-game-events/",
"title": {
"rendered": "Get ready for The Angry Birds Movie 2 premiere with new in-game events!"
},
"excerpt": {
"rendered": "<p>The Angry Birds Movie 2 comes to US theaters tomorrow, but who wants to wait that long?! Good news: you can get into the movie mood right now with a new batch of Angry Birds Movie 2 events in your favorite Angry Birds games! Prime the hype engine with the trailer for The Angry Birds […]</p>\n",
"protected": false
},
"author": 3
},
{
"id": 17447,
"link": "https://www.angrybirds.com/blog/angry-birds-ar-isle-pigs-available-now/",
"title": {
"rendered": "Angry Birds AR: Isle of Pigs is available now!"
},
"excerpt": {
"rendered": "<p>Classic Angry Birds gameplay + AR = an incredible amount of fun! Play Angry Birds AR: Isle of Pigs now on your ARKit enabled iOS device.</p>\n",
"protected": false
},
"author": 3
}
]
当我解析帖子时,它给我**预期的BEGIN_OBJECT,但在第1行第2列路径$**是BEGIN_ARRAY
这是我用来序列化数据的文件。
public class WordPressMain {
private List<WordPressData> data;
public WordPressMain(List<WordPressData> data) {
this.data = data;
}
public List<WordPressData> getData() {
return data;
}
public void setData(List<WordPressData> data) {
this.data = data;
}
}
这也是我的文件用于获取数据,如ID,标题等...
public class WordPressData {
@SerializedName("id")
@Expose
private int id;
@SerializedName("date")
@Expose
private String date;
@SerializedName("title")
@Expose
private WordPressTitle title;
public WordPressData() {
}
public WordPressData(int id, String date, WordPressTitle title) {
this.id = id;
this.date = date;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public WordPressTitle getTitle() {
return title;
}
public void setTitle(WordPressTitle title) {
this.title = title;
}
}
最后一件事这是我的改造类。
public class WordPressApi {
// Parse Url Using Parameters
public static final String BASE_URL = "https://www.angrybirds.com/";
private static Posts posts = null;
public static Posts getMainVideo() {
if (posts == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
posts = retrofit.create(Posts.class);
}
return posts;
}
public interface Posts {
@GET
Call<WordPressMain> getWordPress(@Url String url);
}
}
- 问题是我无法解析数据类型,列表没有名称**
1条答案
按热度按时间mkh04yzy1#
发生这种情况是因为Gson接收到的数据类型与您确定的数据类型不同。
我将分享我的例子检索数据从WordPress的,然后我显示他们在一个RecyclerView我希望它有帮助。
您可以从这个链接获得很好的概述Retrofit
RetroPost类,其中包含我需要从WordPress的字段。
然后是对象的类(如标题和内容),位于内容上方。
然后接口
最后,加载数据的方法