使用Jackson将JSON对象数组反序列化为Java数组时出错

如何解决使用Jackson将JSON对象数组反序列化为Java数组时出错

我正在尝试分析以下JSON文件:https://github.com/russian-ad-explorer/russian-ad-datasets/blob/master/json/russian_ads.json。该文件包含一个JSON对象列表,每个对象都是一个俄罗斯广告,以下是带有前两个对象的前几行:

  {
    "language_categories": [
      "Not Specified"
    ],"placement_categories": [
      "Facebook"
    ],"interests_categories": [],"location_categories": [
      "Midwest","Atlantic"
    ],"ad_id": 331.0,"ad_copy": "\"Free Figure's Black Power Rally at VCU:\"","ad_landing_page": "https://www.facebook.com/Black-Matters-1579673598947501/","ad_targeting_location": "United States: Baltimore Maryland: Ferguson,St. Louis MissouriCleveland Ohio ","age_lower": 18.0,"age_upper": 999,"age": "18-65+","placements": "News Feed on desktop computers or News Feed on mobiledevices ","ad_impressions": 764.0,"ad_clicks": 43.0,"ad_spend_rub": 90.86,"ad_creation_date": "06/15/2015","month": [
      "06"
    ],"year": [
      "2015"
    ],"day": "15","ad_end_date": "06/16/2015","extra_data": "Completed at the Direction of Ranking Memberof the US House Permanent Select Committee on Intelligence  P(1)0002117  \f","pdf_filepath": "2015-06/P10002117.pdf","image_filepath": "missing_ad.png","interest_expansion": null,"excluded_connections": null,"language": null,"ad_spend_usd": 1.66,"efficiency_impressions": 460.24,"efficiency_clicks": 25.9,"conversion_rate": 0.06,"interests_categories_regex": "","location_categories_regex": "Midwest_Atlantic","date_order_index": 41
  },{
    "language_categories": [
      "Not Specified"
    ],"ad_id": 332.0,"ad_copy": "A woman pretended Afro-American to gain control over black communityassociation The president of a local NAACP chapter in Spokane,Washington is under investigation after her parents publicly accused her of lying being African American. Both parents of Rachel Dolezal are white. Rachel Dolezal also serves as the chair of Spokane's Office of Police Ombudsman Commission and is an adjunct professor of Africana Studies at Eastern Washington University. Dolezal falsely identified herself as African-American in order to be appointed as a chair. Dolezal's parents paid attention to the official documents,including a birth certificate,confirming that Rachel is part Swedish,Czech,and German with \"traces\" of Native American heritage. We see,how whites are trying to gain control over traditionally black organization that protects the rights and interests of the black community. In the Dolezal's case it is unlikely it was a mistake or personally motivated fraud. Simply she was given a task to join the association of black community in order to control it and hold the interests of white majority. We need a deep inspection of Rachel Dolezal's activities while she was a president of the local branch of the Association of black people! In order not to repeat similar cases in future we need a careful control of all appointments by black community! As it is,there is a dominance of whites in police departments and authorities of cities with a predominantly black population! ","ad_targeting_location": "United States: Baltimore Maryland; Ferguson,St. Louis Missouri;Cleveland Ohio ","ad_impressions": 676.0,"ad_clicks": 47.0,"ad_spend_rub": 89.51,"extra_data": "Completed at the Direction of Ranking Memberof the US House Permanent Select Committee on Intelligence  P(1)0002118  \f","pdf_filepath": "2015-06/P10002118.pdf","ad_spend_usd": 1.63,"efficiency_impressions": 414.72,"efficiency_clicks": 28.83,"conversion_rate": 0.07,"date_order_index": 42
  },"ad_id": 333.0,"ad_copy": "2Pac believed in FightTupac Shakur was indeed not just one of the greatest rappers of all time but a worldly icon whose status in hip-hop culture can never be replaced. His revolutionary knowledge mixed with street experience made him powerful unstoppable force that spoke to the hearts of millions of people. His songs touch on a number of engaging topics,including religion,women,oppression,etc. 2Pac was a humanitarian,who stood up for those who couldn't stand up for themselves. He believed in Fight and Speaking the Truth. Nowadays a lot of singers,both black and white use their music to sell records,just for making money. American society at all levels became too corruptive,starting from our government and ending with ordinary citizens. On the contrary Tupac did his music to influence the world. That's why he spoke to issues of poverty,oppression and racism. Today young African-Americans have fallen under bad influence of money,drugs and so on. Yeah,it is all started from the government's attitude towards Blacks. But let's remember Tupac and his ability to question the social order. Changes,one of his popular songs,asks everyone to change their lifestyles for better society. He always asked people to share with each other and to learn to love each other. ","ad_impressions": 1075.0,"ad_spend_rub": 174.4,"ad_creation_date": "06/16/2015","day": "16","ad_end_date": "06/17/2015","extra_data": "Completed at the Direction of Ranking Memberof the US House Permanent Select Committee on Intelligence  P(1)0002119  \f","pdf_filepath": "2015-06/P10002119.pdf","ad_spend_usd": 3.21,"efficiency_impressions": 334.89,"efficiency_clicks": 14.64,"conversion_rate": 0.04,"date_order_index": 43
  },

这是我所有的Java代码:

import java.io.IOException;


import com.fasterxml.jackson.databind.ObjectMapper;


public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("src/main/resources/russian_ads.json");
        ObjectMapper mapper  = new ObjectMapper();
        russianAd[] ads = mapper.readValue(file,russianAd[].class);
        System.out.println(ads);
    }
}

和我的POJO:

public class russianAd {
  String[] language_categories;
  String[] placement_categories;
  String[] interest_categories;
  String[] location_categories;
  double ad_id;
  String ad_copy;
  String ad_landing_page;
  String ad_targeting_location;
  double age_lower;
  double age_upper;
  String age;
  String placements;
  double ad_impressions;
  double ad_clicks;
  double ad_spend_rub;
  String ad_creation_date;
  String[] month;
  String[] year;
  String day;
  String ad_end_date;
  String extra_data;
  String pdf_filepath;
  String image_filepath;
  String interest_expansion;
  String excluded_connections;
  String language;
  double ad_spend_usd;
  double efficiency_impressions;
  double efficiency_clicks;
  double conversion_rate;
  String interests_categories_regex;
  String location_categories_regex;
  int date_order_index;

  public russianAd(){

  }
}

但是当我运行它时,出现此错误:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "language_categories" (class com.example.russianAd),not marked as ignorable (0 known properties: ])
 at [Source: (File); line: 3,column: 29] (through reference chain: java.lang.Object[][0]->com.example.russianAd["language_categories"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:840)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1179)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1592)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1570)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:195)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:21)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3070)
    at com.example.Main.main(Main.java:18)

请注意,我不想忽略该变量,并且main()函数也可以访问该变量的范围。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-