如何解决使用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 举报,一经查实,本站将立刻删除。