【问题标题】:Parsing only a part from a large JSON仅解析大型 JSON 的一部分
【发布时间】:2014-05-30 11:57:15
【问题描述】:

我有一个 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;
}


}

【问题讨论】:

  • 您可以访问网络服务代码吗?为什么要发送您不需要的数据?
  • 请贴出导致异常的代码!
  • 那是另一回事了..我知道你是对的,但我无法访问网络服务
  • 你能显示 JSON 并告诉我们你不想映射哪个部分吗?
  • 嗯,文件有点大,看不懂,我用在线JSON查看器格式化后发一张照片

标签: java android json jackson


【解决方案1】:

你不能这样解析它,因为 JSON 中还有其他属性:

Map<String, post[]> map = mapper.readValue(new URL(urls.get(0)), Map.class);

你应该做的是创建一个包含帖子数组的类:

@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:也为 Post 添加了 JsonIgnoreProperties 注释

【讨论】:

  • 这也不起作用..我会把我的课程发给你看看我是否遗漏了什么
  • 在“试用”课程中,您使用“PointOfInterest”课程,但您发布了“帖子”课程
  • 不是这样......我实际上为我的班级使用了这个名字,我只是在stackoverflow的问题中写了帖子。为简单起见
  • 你仍然得到 ClassCastException 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2019-07-18
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多