Gson gson = new Gson();
SearchResult searchResult = gson.fromJson(response, SearchResult.class);
searchResult始终为null。
这是SearchResult类:
public class SearchResult implements Serializable{
public SearchResult() {
}
private List<MyMarkerResponse> myMarkersList;
public List<MyMarkerResponse> getSearch() {
return myMarkersList;
}
public void setSearch(List<MyMarkerResponse> search) {
myMarkersList = search;
}
以下是json的回复:
{"myMarkerList":[{"notes":"now","_id":5096363633147904,"latitude":51.52753303816573,"longitude":-0.15742387622594833,"title":"home"},{"notes":"","_id":5137355874762752,"latitude":51.46299731478184,"longitude":-0.015837103128433228,"title":""},{"notes":"","_id":5167132077719552,"latitude":44.890621596087136,"longitude":42.57036656141281,"title":"cat"},{"notes":"new place","_id":5631986051842048,"latitude":65.7773746361831,"longitude":107.60726854205132,"title":"hello"},{"notes":"new place","_id":5692462144159744,"latitude":65.7773746361831,"longitude":107.60726854205132,"title":"hello"},{"notes":"","_id":5720147234914304,"latitude":51.51407752981666,"longitude":-0.12392342090606688,"title":""},{"notes":"place","_id":5730082031140864,"latitude":61.10287229393116,"longitude":88.51000271737576,"title":"new"},{"notes":"","_id":5749328048029696,"latitude":23.44142818293187,"longitude":20.003406554460526,"title":""},{"notes":"now","_id":5769015641243648,"latitude":51.52753303816573,"longitude":-0.15742387622594833,"title":"home"}]}
这是MyMarkerResponse类:
public class MyMarkerResponse implements Serializable{
public MyMarkerResponse() {
}
private String notes;
private Long _id;
private double latitude;
private double longitude;
private String title;
public Long get_id() {
return _id;
}
public void set_id(Long _id) {
this._id = _id;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
}
当我调用searchResult.getSearch()时,我收到此错误:
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
如果你能提供帮助,请!!
答案 0 :(得分:0)
您的SearchResult
POJO对于您希望反序列化的JSON不正确。具体来说,主JSON对象只包含一个名为&#34; myMarkerList
&#34;的属性/属性/键。因此,您的POJO应该同样包含list / array类型的一个属性。您需要将其更改为:
public class SearchResult {
private List<MyMarkerResponse> myMarkerList;
public List<MyMarkerResponse> getSearch() {
return myMarkerList;
}
public void setSearch(List<MyMarkerResponse> search) {
Search = myMarkerList;
}
}
<强>更新:强>
我意识到代码本身是正确的,您只需要使用不同的fromJson
函数(将Type
作为第二个参数的函数):
Gson gson = new Gson();
Type searchResultType = new TypeToken<SearchResult>() {}.getType();
SearchResult searchResult = gson.fromJson(response, searchResultType);
然后您的代码应该按预期工作。 请试一试,如果有帮助请告诉我。
答案 1 :(得分:0)
答案 2 :(得分:0)
您在POJO中拼错了myMarkerList
。目前你有:
private List<MyMarkerResponse> myMarkersList;
检查您的拼写(删除&#39;)。