繁体   English   中英

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

[英]Parsing a Very Complex JSON file with Jackson Library in Java

我搜索了许多论坛,问题和教程,以了解如何将以下JSON文件解析为Java对象。 目前,我已经尝试将数据完全绑定到另一个类,但是我不想将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 Object(在这种情况下为类)。

但我不想将JSON文件中的所有密钥放入该类中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM