gpt4 book ai didi

java - 无法从 START_ARRAY token 中反序列化 java.lang.String 的实例

转载 作者:IT老高 更新时间:2023-10-28 20:53:31 26 4
gpt4 key购买 nike

我对 Jackson 解析器非常陌生。我的代码运行良好,直到今天。我无法找出错误。

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: line: 1, column: 1095] (through reference chain: JsonGen["platforms"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:163)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:588)
at com.fasterxml.jackson.databind.deser.std.JdkDeserializers$StringDeserializer.deserialize(JdkDeserializers.java:90)
at com.fasterxml.jackson.databind.deser.std.JdkDeserializers$StringDeserializer.deserialize(JdkDeserializers.java:59)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:336)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:89)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:290)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2563)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1789)
at BossMan.candie(BossMan.java:49)
at Hilda.main(Hilda.java:81)

这里是 URL 调用和对象映射器的代码:

public class BossMan {
public String[] candie () throws JsonParseException, JsonMappingException, IOException
{
URL jsonUrl = new URL("http://www.dishanywhere.com/radish/v20/dol/home/carousels/shows.json");

ObjectMapper objmapper = new ObjectMapper();
List<JsonGen> jsongen = objmapper.readValue(jsonUrl, new TypeReference<List<JsonGen>>() {});
String[] shows = new String [jsongen.size()];
int i = 0;
for(JsonGen element : jsongen) {
shows[i++]=element.getName();
}
return shows;
}
}

这里是 POJO 类:

import java.util.List;

public class JsonGen{
private String _type;
private List cast;
private List clips;
private Common_sense_data common_sense_data;
private String common_sense_id;
private List crew;
private String description;
private List episodes;
private Number franchise_id;
private List genres;
private String guid;
private Images images;
private boolean is_locked;
private boolean is_mobile;
private boolean is_parental_locked;
private String kind;
private String mobile_networks;
private String most_recent_full_episode_added_date;
private String name;
private List networks;
private String platforms;
private List ratings;
private String release_date;
private List season_filters;
private String slug;
private String tms_id;

public String get_type(){
return this._type;
}
public void set_type(String _type){
this._type = _type;
}
public List getCast(){
return this.cast;
}
public void setCast(List cast){
this.cast = cast;
}
public List getClips(){
return this.clips;
}
public void setClips(List clips){
this.clips = clips;
}
public Common_sense_data getCommon_sense_data(){
return this.common_sense_data;
}
public void setCommon_sense_data(Common_sense_data common_sense_data){
this.common_sense_data = common_sense_data;
}
public String getCommon_sense_id(){
return this.common_sense_id;
}
public void setCommon_sense_id(String common_sense_id){
this.common_sense_id = common_sense_id;
}
public List getCrew(){
return this.crew;
}
public void setCrew(List crew){
this.crew = crew;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public List getEpisodes(){
return this.episodes;
}
public void setEpisodes(List episodes){
this.episodes = episodes;
}
public Number getFranchise_id(){
return this.franchise_id;
}
public void setFranchise_id(Number franchise_id){
this.franchise_id = franchise_id;
}
public List getGenres(){
return this.genres;
}
public void setGenres(List genres){
this.genres = genres;
}
public String getGuid(){
return this.guid;
}
public void setGuid(String guid){
this.guid = guid;
}
public Images getImages(){
return this.images;
}
public void setImages(Images images){
this.images = images;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public boolean getIs_mobile(){
return this.is_mobile;
}
public void setIs_mobile(boolean is_mobile){
this.is_mobile = is_mobile;
}
public boolean getIs_parental_locked(){
return this.is_parental_locked;
}
public void setIs_parental_locked(boolean is_parental_locked){
this.is_parental_locked = is_parental_locked;
}
public String getKind(){
return this.kind;
}
public void setKind(String kind){
this.kind = kind;
}
public String getMobile_networks(){
return this.mobile_networks;
}
public void setMobile_networks(String mobile_networks){
this.mobile_networks = mobile_networks;
}
public String getMost_recent_full_episode_added_date(){
return this.most_recent_full_episode_added_date;
}
public void setMost_recent_full_episode_added_date(String most_recent_full_episode_added_date){
this.most_recent_full_episode_added_date = most_recent_full_episode_added_date;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List getNetworks(){
return this.networks;
}
public void setNetworks(List networks){
this.networks = networks;
}
public String getPlatforms(){
return this.platforms;
}
public void setPlatforms(String platforms){
this.platforms = platforms;
}
public List getRatings(){
return this.ratings;
}
public void setRatings(List ratings){
this.ratings = ratings;
}
public String getRelease_date(){
return this.release_date;
}
public void setRelease_date(String release_date){
this.release_date = release_date;
}
public List getSeason_filters(){
return this.season_filters;
}
public void setSeason_filters(List season_filters){
this.season_filters = season_filters;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getTms_id(){
return this.tms_id;
}
public void setTms_id(String tms_id){
this.tms_id = tms_id;
}
}

这里是主要功能:

public class Hilda {
public static boolean b ;
public static WebDriver driver;
public static int countingCrows(WebDriver driver) throws InterruptedException{
int i,j=0,k=0;
for(i=1; i<=2; i++){
for(j=1; j<=6; j++){
if(!(i==1&&j==6)){
b = driver.findElement(By.xpath("//div[2]/div[5]/div[3]/div/div/div/div[1]/div/div[2]/div[2]/div/div["+i+"]/ul/li["+j+"]/div/a/img")).isEnabled();
k++;
//System.out.println("k inner loop:"+k);
}
if(i==1&&j==5){
Thread.sleep(5000);
try{
boolean c = driver.findElement(By.xpath("//div[2]/div[5]/div[3]/div/div/div/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[6]/div/a/img")).isEnabled();
if(c){
k++;
//System.err.println("k inside j==5 for sixsth element:"+k);
}
}catch (Exception e) {
System.err.println("sixsth elemnet not found in first row");
}
}
}
if(i==1&&j==5){
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@class='carousel-control left']")).click();
}
}
System.out.println("total no :"+k);
return k;
}

public static String[] unchained(WebDriver driver) throws InterruptedException{
Thread.sleep(5000);
int j = countingCrows(driver);
int i,k=0;
String[] singleText1 = new String[11];
if(j==11){
for(i=1; i<=2; i++){
for(j=1; j<=6; j++){
if(!(i==1&&j==6)){
singleText1[k] = driver.findElement(By.xpath("//div[2]/div[5]/div[3]/div/div/div/div[1]/div/div[2]/div[2]/div/div["+i+"]/ul/li["+j+"]/div/a/img")).getAttribute("id");
//boolean b = driver.findElement(By.xpath("//div[2]/div[5]/div[3]/div/div/div/div[1]/div/div[2]/div[2]/div/div["+i+"]/ul/li["+j+"]/div/a/img")).isEnabled();
//System.out.print(j-1+"-"+singleText1[k]); System.out.println("-"+b);
k++;
}else {
System.err.println("element div[1] div[6] is not present");
}
}
if(j==5){
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@class='carousel-control left']")).click();
}
}
}
return singleText1;
}

public static void main(String[] args) throws InterruptedException, JsonParseException, JsonMappingException, IOException {
WebDriver driver = new SafariDriver();
driver.get("http://www.dishanywhere.com");
Thread.sleep(5000);
String [] cottonSack =unchained(driver);
System.out.println("*******************************************************");
int j = countingCrows(driver);
/*for(int i= 0;i<=j-1;i++){
System.out.println(cottonSack[i]);
}*/

BossMan calvin = new BossMan();
String[] dogs = calvin.candie();
for(int k=0;k<cottonSack.length;k++){
boolean c = dogs[k].equalsIgnoreCase(cottonSack[k]);
if(c){
System.out.println("Comparing json data- " +dogs[k]+" with webpage data- "+cottonSack[k]+" and Result is- "+c);
}else {
System.out.println("Comparing json data- " +dogs[k]+" with webpage data- "+cottonSack[k]+" Result- "+c);
}
}
}
}

最佳答案

错误是:

Can not deserialize instance of java.lang.String out of START_ARRAY token at [Source: line: 1, column: 1095] (through reference chain: JsonGen["platforms"])

在 JSON 中,platforms 如下所示:

"platforms": [
{
"platform": "iphone"
},
{
"platform": "ipad"
},
{
"platform": "android_phone"
},
{
"platform": "android_tablet"
}
]

所以试着把你的 pojo 改成这样:

private List platforms;

public List getPlatforms(){
return this.platforms;
}

public void setPlatforms(List platforms){
this.platforms = platforms;
}

编辑:您也需要更改 mobile_networks。看起来像这样:

private List mobile_networks;

public List getMobile_networks() {
return mobile_networks;
}

public void setMobile_networks(List mobile_networks) {
this.mobile_networks = mobile_networks;
}

关于java - 无法从 START_ARRAY token 中反序列化 java.lang.String 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27911591/

26 4 0