Java 使用gson反序列化JSON对象数组时出现问题
我正在编写一个android reader应用程序,它使用wordpress REST API从wordpress.com站点提取内容,该API返回JSON对象,我正在将其反序列化为应用程序中定义的文章对象。以下代码用于获取单个帖子的数据,工作正常:Java 使用gson反序列化JSON对象数组时出现问题,java,android,json,wordpress,gson,Java,Android,Json,Wordpress,Gson,我正在编写一个android reader应用程序,它使用wordpress REST API从wordpress.com站点提取内容,该API返回JSON对象,我正在将其反序列化为应用程序中定义的文章对象。以下代码用于获取单个帖子的数据,工作正常: private class getOne extends AsyncTask <Void, Void, JSONObject> { private static final String url = "https://p
private class getOne extends AsyncTask <Void, Void, JSONObject> {
private static final String url = "https://public-api.wordpress.com/rest/v1/sites/drewmore4.wordpress.com/posts/slug:good-one";
@Override
protected JSONObject doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("accept", "application/json");
HttpResponse response;
JSONObject object = new JSONObject();
String resprint = new String();
try {
response = httpclient.execute(httpget);
// Get the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
// get entity contents and convert it to string
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
resprint = result;
// construct a JSON object with result
object=new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
}
}
catch (ClientProtocolException e) {System.out.println("CPE"); e.printStackTrace();}
catch (IOException e) {System.out.println("IOE"); e.printStackTrace();}
catch (JSONException e) { System.out.println("JSONe"); e.printStackTrace();}
return object;
}
@Override
protected void onPostExecute (JSONObject object){
System.out.println("POSTStexxx");
Gson gson = new Gson();
Article a = gson.fromJson(object.toString(), Article.class);
System.out.println("XXCONTENT: " + a.content);
System.out.println(a.ID);
System.out.println(a.title);
System.out.println(a.author.name);
// System.out.println(a.attachments.URL);
WebView wv = (WebView)findViewById(R.id.mainview);
wv.loadDataWithBaseURL(url, a.content, "text/html", "UTF-8", null);
wv.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
}
}
以及PostsHandler类,所有帖子的查询响应都映射到该类(我怀疑我的问题出在这里):
所有未标记@SerializedName注释的字段都与JSONObject中使用的字段相同
我正在使用的JSONObject可以在以下位置看到:
GSON在序列化/反序列化信息时支持“强”和“弱”类型的概念。强类型用定义良好的接口表示实际的JavaBean对象。弱类型表示数据(键/值)对的映射。目前,您正在尝试混合和匹配这两种模型,但这不起作用。您要求GSON将数据反序列化为“强”类型(
PostsHandler
)。但在该类中,您存储的是GSON的“弱”类型的实例(即JSONObjects
)。您应该选择(并坚持)一种处理模型。假设我们将使用强类型来反序列化数据
这就是我如何实现PostsHandler的方法:
public PostsHandler implements Serializable {
@SerializedName("found")
private int number;
@SerializedName("posts")
private List<Article> articles
// Constructors, getters, setters
}
美丽,完美。谢谢你的解释,我只是在熟悉gson,这真的很有帮助。没问题,祝你在学习过程中好运。Gson是一个很好的图书馆。是的,到目前为止,它很容易学习,除了几个小问题。说到这里,我还有一个问题:()。我想知道这是否是我应该利用弱类型的一个例子?@drewmore4-是的,这将是弱类型的一个很好的候选者。
public class Article implements Serializable {
// private static final long serialVersionUID = 1L;
int ID;
public String title;
public String excerpt;
public Author author;
public String date;
public String URL;
@SerializedName("featured_image")
public String image;
public String content;
//public String[] attachments;
public Attachment attachments;
public int comment_count;
public int like_count;
}
class Author {
long id;
String name;
String URL;
}
public class PostsHandler {
int number;
JSONObject[] posts;
}
public PostsHandler implements Serializable {
@SerializedName("found")
private int number;
@SerializedName("posts")
private List<Article> articles
// Constructors, getters, setters
}
@Override
protected void onPostExecute (JSONObject returned) {
Gson gson = new Gson();
PostsHandler ph = gson.fromJson(returned.toString(), PostsHandler.class);
System.out.println("Article array length: " + ph.getArticles().size());
stories = arts;
populateUI();
}