gpt4 book ai didi

java - 仅解析大型 JSON 的一部分

转载 作者:行者123 更新时间:2023-12-02 05:47:11 25 4
gpt4 key购买 nike

enter image description here我有一个 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 中还有其他属性:

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

关于java - 仅解析大型 JSON 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23954058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com