gpt4 book ai didi

java - 使用 Java 中的 Jackson 库解析非常复杂的 JSON 文件

转载 作者:行者123 更新时间:2023-12-01 12:57:19 24 4
gpt4 key购买 nike

我搜索了许多论坛、问题和教程,以了解如何将下面的 JSON 文件解析为 java 对象。目前,我已尝试将完整数据绑定(bind)到另一个类,但我不想将 JSON 文件中的所有键放入该类中。鉴于这个问题,我尝试以树的形式读入并以这种方式浏览它,但我也无法使其工作。

以下是 JSON 文件的示例:

{
"tweets": [
{
"filter_level": "medium",
"retweeted_status": {
"contributors": null,
"text": "It's winter, right?",
"geo": null,
"retweeted": false,
"in_reply_to_screen_name": null,
"truncated": false,
"lang": "en",
"entities": {
"symbols": [

],
"urls": [

],
"hashtags": [

],
"user_mentions": [

]
},
"in_reply_to_status_id_str": null,
"id": 419133079765016576,
"source": "<a href=\"http://www.echofon.com/\" rel=\"nofollow\">Echofon<\/a>",
"in_reply_to_user_id_str": null,
"favorited": false,
"in_reply_to_status_id": null,
"retweet_count": 2,
"created_at": "Fri Jan 03 15:48:07 +0000 2014",
"in_reply_to_user_id": null,
"favorite_count": 0,
"id_str": "419133079765016576",
"place": null,
"user": {
"location": "",
"default_profile": false,
"profile_background_tile": true,
"statuses_count": 55398,
"lang": "en",
"profile_link_color": "E03019",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/32943506/1354602333",
"id": 32943506,
"following": null,
"protected": false,
"favourites_count": 168,
"profile_text_color": "F22805",
"description": "IG: baldhead_jay",
"verified": false,
"contributors_enabled": false,
"profile_sidebar_border_color": "FFFFFF",
"name": "Jazzmen",
"profile_background_color": "080808",
"created_at": "Sat Apr 18 17:06:09 +0000 2009",
"default_profile_image": false,
"followers_count": 919,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/418581792082239488/qqmW8FDf_normal.jpeg",
"geo_enabled": false,
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/712526073/ff1708a70b892c6635a263a66c592216.jpeg",
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/712526073/ff1708a70b892c6635a263a66c592216.jpeg",
"follow_request_sent": null,
"url": null,
"utc_offset": -28800,
"time_zone": "Pacific Time (US & Canada)",
"notifications": null,
"profile_use_background_image": true,
"friends_count": 802,
"profile_sidebar_fill_color": "0A0A09",
"screen_name": "_PumpsAndJays",
"id_str": "32943506",
"profile_image_url": "http://pbs.twimg.com/profile_images/418581792082239488/qqmW8FDf_normal.jpeg",
"listed_count": 4,
"is_translator": false
},
"coordinates": null
},
"contributors": null,
"text": "RT @_PumpsAndJays: It's winter, right? I'm going to need yal to shut the hell up about all this snow! 😒",
"geo": null,
"retweeted": false,
"in_reply_to_screen_name": null,
"truncated": false,
"lang": "en",
"entities": {
"symbols": [

],
"urls": [

],
"hashtags": [

],
"user_mentions": [
{
"id": 32943506,
"name": "Jazzmen",
"indices": [
3,
17
],
"screen_name": "_PumpsAndJays",
"id_str": "32943506"
}
]
},
"in_reply_to_status_id_str": null,
"id": 419137024818282496,
"source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android<\/a>",
"in_reply_to_user_id_str": null,
"favorited": false,
"in_reply_to_status_id": null,
"retweet_count": 0,
"created_at": "Fri Jan 03 16:03:48 +0000 2014",
"in_reply_to_user_id": null,
"favorite_count": 0,
"id_str": "419137024818282496",
"place": null,
"user": {
"location": "Chicago",
"default_profile": false,
"profile_background_tile": false,
"statuses_count": 22140,
"lang": "en",
"profile_link_color": "1CA5C7",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/88852465/1374562212",
"id": 88852465,
"following": null,
"protected": false,
"favourites_count": 77,
"profile_text_color": "030103",
"description": "A graduate from Central State University. I'm a laid back kind of girl.",
"verified": false,
"contributors_enabled": false,
"profile_sidebar_border_color": "081012",
"name": "Renee",
"profile_background_color": "AB0D28",
"created_at": "Tue Nov 10 04:40:41 +0000 2009",
"default_profile_image": false,
"followers_count": 263,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/378800000596965744/512ee5834ca9977193deb181d0130d33_normal.jpeg",
"geo_enabled": true,
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/383615087/paris.jpg",
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/383615087/paris.jpg",
"follow_request_sent": null,
"url": null,
"utc_offset": -21600,
"time_zone": "Central Time (US & Canada)",
"notifications": null,
"profile_use_background_image": true,
"friends_count": 390,
"profile_sidebar_fill_color": "690966",
"screen_name": "SuitNTaiShyt",
"id_str": "88852465",
"profile_image_url": "http://pbs.twimg.com/profile_images/378800000596965744/512ee5834ca9977193deb181d0130d33_normal.jpeg",
"listed_count": 2,
"is_translator": false
},
"coordinates": null
},
{
"filter_level": "medium",
"contributors": null,
"text": "U know your ice fishing at home when ur snacks are sandwiches,chips,dips,beef jerky and all the goodies instead of just a case of beer #mom",
"geo": null,
"retweeted": false,
"in_reply_to_screen_name": null,
"truncated": false,
"lang": "en",
"entities": {
"symbols": ["id"

],
"urls": [

],
"hashtags": [
{
"text": "mom",
"indices": [
135,
139
]
}
],
"user_mentions": [

]
},
"in_reply_to_status_id_str": null,
"id": 419137025376145408,
"source": "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone<\/a>",
"in_reply_to_user_id_str": null,
"favorited": false,
"in_reply_to_status_id": null,
"retweet_count": 0,
"created_at": "Fri Jan 03 16:03:48 +0000 2014",
"in_reply_to_user_id": null,
"favorite_count": 0,
"id_str": "419137025376145408",
"place": null,
"user": {
"location": "",
"default_profile": false,
"profile_background_tile": true,
"statuses_count": 5411,
"lang": "en",
"profile_link_color": "17EBCF",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/408240224/1384838946",
"id": 408240224,
"following": null,
"protected": false,
"favourites_count": 4222,
"profile_text_color": "333333",
"description": "It is what it is, no more, no less. Trust in the Lord. BSU Softball #5. My whole being is happy - Psalm 16:9",
"verified": false,
"contributors_enabled": false,
"profile_sidebar_border_color": "FFFFFF",
"name": "Nicole Anne",
"profile_background_color": "C0DEED",
"created_at": "Wed Nov 09 05:03:58 +0000 2011",
"default_profile_image": false,
"followers_count": 452,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/413270299925164033/mWiJBKP2_normal.jpeg",
"geo_enabled": true,
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/698793822/14d9ce538d96b435ad4449d5385a3ec1.jpeg",
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/698793822/14d9ce538d96b435ad4449d5385a3ec1.jpeg",
"follow_request_sent": null,
"url": null,
"utc_offset": -32400,
"time_zone": "Alaska",
"notifications": null,
"profile_use_background_image": true,
"friends_count": 634,
"profile_sidebar_fill_color": "DDEEF6",
"screen_name": "NDupay",
"id_str": "408240224",
"profile_image_url": "http://pbs.twimg.com/profile_images/413270299925164033/mWiJBKP2_normal.jpeg",
"listed_count": 1,
"is_translator": false
},
"coordinates": null
},

正如你所见,乍一看非常复杂;不过,我写了这么多是为了向您展示一些东西。这是一个示例 json 文件,代表来自 Twitter 的推文,但没有一个是真实的。

这是我的主要解析代码:

private static File twitterFile;

public TweetJSONParser(File twitterFile) {
TweetJSONParser.twitterFile = twitterFile;
}

private static String jsonFile() {

String jsonFile = twitterFile.getName();
return jsonFile;
}

public Tweet getTweet() {

ObjectMapper mapper = new ObjectMapper();
Tweet tweet = new WeatherTweet();

try {
tweet = mapper.readValue(new File(jsonFile()), WeatherTweet.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}

return tweet;
}

这是我一直在使用的类:

public class Tweet {

private String source;
private Boolean retweeted;
private int retweetCount;
private int contributors;
private Boolean favorited;
private int favoriteCount;
private String id;
private String text;
private String time;
private String location;
private String coordinates;
private String classification = null;

ArrayList<String> userMentions;
ArrayList<String> hashtags;
//ArrayList<String> symbols;

//Constructor
public Tweet() {
retweeted = false;
favorited = false;

hashtags = new ArrayList<String>();
userMentions = new ArrayList<String>();

}

public String getClassification() {
return classification;
}

public void setClassification(String classification) {
this.classification = classification;
}


public String getId() {
return id;
}

public void setid(String id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getCoordinates() {
return coordinates;
}

public void setCoordinates(String coordinates) {
this.coordinates = coordinates;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public void setRetweeted(Boolean retweeted) {
this.retweeted = retweeted;
}

public Boolean isRetweeted() {
return retweeted;
}

public int getContributors() {
return contributors;
}

public void setContributors(int contributors) {
this.contributors = contributors;
}

public int getRetweetCount() {
return retweetCount;
}

public void setRetweetCount(int retweetCount) {
this.retweetCount = retweetCount;
}

public void setFavorited(Boolean favorited) {
this.favorited = favorited;
}

public Boolean isFavorited() {
return favorited;
}

public int getFavoriteCount() {
return favoriteCount;
}

public void setFavoriteCount(int favoriteCount) {
this.favoriteCount = favoriteCount;
}

public class Entities {

public class UserMentions {

private String user;

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public void addUserMention(String user) {
userMentions.add(user);
}

public String getUserMention(int index) {
return userMentions.get(index);
}

public Integer getNumUserMentions() {
return userMentions.size();
}
}

public class hashtags {

private String hashtag;

public String getHashtag() {
return hashtag;
}

public void setHashtag(String hashtag) {
this.hashtag = hashtag;
}

public void addHashtag(String hashtag) {
hashtags.add(hashtag);
}

public String getHashtag(Integer index) {
return hashtags.get(index);
}

public Integer getNumHashtags() {
return hashtags.size();
}
}
}

public class User {

private String user;
private String location;
private String timeZone;
private String id;
private String description;
private String createdAt;
private int statusesCount;
private int favouritesCount;
private int followersCount;
private Boolean geoEnabled;
private String url;
private int utcOffset;
private int friendsCount;

public String getTimeZone() {
return timeZone;
}

public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}

public int getFavouritesCount() {
return favouritesCount;
}

public void setFavouritesCount(int favouritesCount) {
this.favouritesCount = favouritesCount;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public int getStatusesCount() {
return statusesCount;
}

public void setStatusesCount(int statusesCount) {
this.statusesCount = statusesCount;
}

public int getFollowersCount() {
return followersCount;
}

public void setFollowersCount(int followersCount) {
this.followersCount = followersCount;
}

public Boolean getGeoEnabled() {
return geoEnabled;
}

public void setGeoEnabled(Boolean geoEnabled) {
this.geoEnabled = geoEnabled;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getUtcOffset() {
return utcOffset;
}

public void setUtcOffset(int utcOffset) {
this.utcOffset = utcOffset;
}

public int getFriendsCount() {
return friendsCount;
}

public void setFriendsCount(int friendsCount) {
this.friendsCount = friendsCount;
}
}

}

我的问题是:我只想将未转发的推文放入推文java对象(PO​​JO)中,即那些在第一个字段“filter_medium”之后没有“retweeted_status”字段的推文,以及哪些内容我有它不起作用。任何人都可以提供具体的帮助吗?我希望这一切都有意义,但由于两个小时前我对 JSON 完全陌生,而且我对如何执行此操作感到困惑。

编辑:

正如其中一条评论所指出的,我对措辞表示歉意。根本问题是我不知道如何解析我提供的 JSON 文件。到目前为止,我解析所拥有内容的努力并未将数据正确排序到类中,因为我不明白如何使用 JSON 解析库来解析这个复杂的文件。对于我目前对该主题的了解来说,这有点太复杂了,我正在寻求您的帮助,了解如何使用 Jackson 库或任何其他合适的库,以正确将此文件解析为 Java 对象(在本例中为类)。

最佳答案

but I don't want to put all of the keys from the JSON file into that class

如果您想将 JSON 数据解析为 JSON 数据,则必须以某种方式定义要从 JSON 数据中提取的字段。我建议您首先学习如何使用 Android 中默认(内置)解析器解析 JSON。一旦你这样做了,你就不会再将你的示例视为“复杂的 json”。

关于java - 使用 Java 中的 Jackson 库解析非常复杂的 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770434/

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