I have a URL which contains contains a JSON array of some objects that I need for my android application.I've written the mapping class for these objects and also added
@JsonIgnoreProperties(ignoreUnknown = true)
for some fields that are unimportant for my end.My problem is that other than my precious data this URL also contains some extra and unnecessary information that I do not need to parse nor create a mapping class for in my part. I'm also using Jackson for quick parsing the JSON. So my question: how can I parse the whole URL but map only the desired JSON Array?
ObjectMapper mapper = new ObjectMapper();
Map<String,post[]> map = mapper.readValue(new URL(urls.get(0)), Map.class);
System.out.println(map.get("posts").length);
what I've written above compiles just fine but when I try to get the array of posts I get java.lang.ClassCastException: java.util.ArrayList
here's my code
@Override
protected String doInBackground(String... params) {
try {
Trial obj = mapper.readValue(new URL(urls.get(0)), Trial.class);
System.out.println(obj.pois.size());
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "done";
}
Trial class
@JsonIgnoreProperties(ignoreUnknown = true)
public class Trial {
public String status;
public String count;
public String pages;
public ArrayList<PointOfInterest> pois;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getPages() {
return pages;
}
public void setPages(String pages) {
this.pages = pages;
}
public ArrayList<PointOfInterest> getPois() {
return pois;
}
public void setPois(ArrayList<PointOfInterest> pois) {
this.pois = pois;
}
}
Post Class
@JsonIgnoreProperties(ignoreUnknown = true)
public class Post{
public String title;
public String content;
public String id;
public String categoryIdFirstLevel;
public String categoryIdSecondLevel;
public String categoryIdThirdLevel;
public String categoryIdFourthLevel;
public String latitude;
public String longitude;
public Post(){
}
public Post(String title,String content,String id,String first,String second,String third,String fourth,String lat,String lon,ArrayList<String> urls){
this.title=title;
this.content=content;
this.id=id;
this.categoryIdFirstLevel=first;
this.categoryIdSecondLevel=second;
this.categoryIdThirdLevel=third;
this.categoryIdFourthLevel=fourth;
this.latitude=lat;
this.longitude=lon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategoryIdFirstLevel() {
return categoryIdFirstLevel;
}
public void setCategoryIdFirstLevel(String categoryIdFirstLevel) {
this.categoryIdFirstLevel = categoryIdFirstLevel;
}
public String getCategoryIdSecondLevel() {
return categoryIdSecondLevel;
}
public void setCategoryIdSecondLevel(String categoryIdSecondLevel) {
this.categoryIdSecondLevel = categoryIdSecondLevel;
}
public String getCategoryIdThirdLevel() {
return categoryIdThirdLevel;
}
public void setCategoryIdThirdLevel(String categoryIdThirdLevel) {
this.categoryIdThirdLevel = categoryIdThirdLevel;
}
public String getCategoryIdFourthLevel() {
return categoryIdFourthLevel;
}
public void setCategoryIdFourthLevel(String categoryIdFourthLevel) {
this.categoryIdFourthLevel = categoryIdFourthLevel;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
}