我正在将json对象转换为java对象,但我一直在收到错误 com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第1行第20行的未终止对象路径$ [0] .urlToImage
这是我的代码
public void onResponse(JSONObject response) {
JSONArray recArticles = new JSONArray();
try {
recArticles = response.getJSONArray("articles");
for (int i=0;i<recArticles.length();i++) {
JSONObject jsonObject = recArticles.getJSONObject(i);;
String author = jsonObject.getString("author");
String title = jsonObject.getString("title");
String description = jsonObject.getString("description");
String url = jsonObject.getString("url");
String urlToImage = jsonObject.getString("urlToImage");
HashMap<String, String> singleArticle = new HashMap<String, String>();
singleArticle.put("author", author);
singleArticle.put("title", title);
singleArticle.put("description", description);
singleArticle.put("url", url);
singleArticle.put("urlToImage", urlToImage);
mArticleList.add(String.valueOf(singleArticle));
}
} catch (JSONException e) {
e.printStackTrace();
}
Gson gson = new Gson();
Type articleListType = new TypeToken<Collection<RecArticle>>() {}.getType();
Log.d(TAG, "onResponse: " + mArticleList);
Log.d(TAG, "onResponse: " + String.valueOf(mArticleList));
mArticles = gson.fromJson(String.valueOf(mArticleList), articleListType);
mArticleAdapter = new RecommenderAdapter(getContext(), mArticles);
mRecyclerView.setAdapter(mArticleAdapter);
mAviLib.hide();
mRefreshLayout.setRefreshing(false);
}
答案 0 :(得分:0)
您的Json回复似乎是正确的。
但是为什么你要用gson和其他一半手动解析你的一半?完全没有逻辑。
使用它就像那样:
<强> Article.java 强>
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Article {
@SerializedName("author")
@Expose
private String author;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("url")
@Expose
private String url;
@SerializedName("urlToImage")
@Expose
private String urlToImage;
@SerializedName("publishedAt")
@Expose
private String publishedAt;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrlToImage() {
return urlToImage;
}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
}
ResponseModel.java
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ResponseModel {
@SerializedName("status")
@Expose
private String status;
@SerializedName("source")
@Expose
private String source;
@SerializedName("sortBy")
@Expose
private String sortBy;
@SerializedName("articles")
@Expose
private List<Article> articles = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getSortBy() {
return sortBy;
}
public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
}
并解析如下:
public void onResponse(JSONObject response) {
Gson gson = new Gson();
ResponseModel responseModel = gson.fromJson(response.toString(), ResponseModel.class);
List<Article> articles = responseModel.getArticles();
mArticleAdapter = new RecommenderAdapter(getContext(), articles);
mRecyclerView.setAdapter(mArticleAdapter);
mAviLib.hide();
mRefreshLayout.setRefreshing(false);
}