简体   繁体   English

仅解析大型JSON的一部分

[英]Parsing only a part from a large JSON

在此处输入图片说明 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 我有一个URL,其中包含我的android应用程序所需的一些对象的JSON数组。我已经为这些对象编写了映射类,并且还添加了

@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. 我的问题是,除了宝贵的数据外,此URL还包含一些额外的和不必要的信息,这些信息不需要我自己解析或创建映射类。 I'm also using Jackson for quick parsing the JSON. 我还使用Jackson来快速解析JSON。 So my question: how can I parse the whole URL but map only the desired JSON Array? 所以我的问题是:如何解析整个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);

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 我上面编写的内容可以正常编译,但是当我尝试获取帖子数组时,我得到了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;
}


}

You can't parse it like this because there are other properties also in JSON: 您无法像这样解析它,因为JSON中还有其他属性:

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

What you should do is create a class that contains the posts array: 您应该做的是创建一个包含posts数组的类:

@JsonIgnoreProperties(ignoreUnknown = true)
static class Post {
    public int id;
    public String type;
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Mapper {
    public ArrayList<Post> posts;
}

and then parse the JSON: 然后解析JSON:

Mapper obj = mapper.readValue(json, Mapper.class);

Edit1: added JsonIgnoreProperties annotation to Post too Edit1:也向Post添加了JsonIgnoreProperties批注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM