我有一个URL,其中包含我的android应用程序所需的一些JSON数组。我已经为这些对象编写了映射类并添加了
@JsonIgnoreProperties(ignoreUnknown = true)
对于一些对我来说不重要的字段。我的问题是除了我的宝贵数据之外,这个URL还包含一些额外的和不必要的信息,我不需要解析这些信息,也不需要为我创建一个映射类。我也使用Jackson快速解析JSON。所以我的问题是:如何解析整个URL,但只映射所需的JSON数组?
ObjectMapper mapper = new ObjectMapper();
Map<String,post[]> map = mapper.readValue(new URL(urls.get(0)), Map.class);
System.out.println(map.get("posts").length);
我上面写的内容编译得很好但是当我尝试获取帖子数组时,我得到了java.lang.ClassCastException: java.util.ArrayList
这是我的代码
@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";
}
试用课程
@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;
}
}
发布课程
@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;
}
}
答案 0 :(得分:2)
你不能像这样解析它,因为JSON中还有其他属性:
Map<String, post[]> map = mapper.readValue(new URL(urls.get(0)), Map.class);
你应该做的是创建一个包含posts数组的类:
@JsonIgnoreProperties(ignoreUnknown = true)
static class Post {
public int id;
public String type;
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Mapper {
public ArrayList<Post> posts;
}
然后解析JSON:
Mapper obj = mapper.readValue(json, Mapper.class);
Edit1:将JsonIgnoreProperties注释添加到Post