来自json字符串的特定信息

时间:2014-02-08 18:42:48

标签: java json twitter

我已经在Eclipse中玩了一段时间的推文,它们以json字符串形式呈现。

为此,我创建了一个名为Tweet(original,huh?)的对象,该对象从json字符串中获取某些信息,并将其存储在Tweet对象中。没什么好看的。

我的推文类如下:

public class Tweet implements TwitterMelding {

public Tweet() {
}

String created_at;
String id;
String text;
String user;

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

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

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

public void setCreated_at(String created_at) {
    this.created_at = created_at;
}
}

现在,看起来简单,有一个不起作用。

特别是 String用户。它应该做的是存储发布推文的人的用户ID。

以下是从推特上获得的推文,其长度非常可怕:

{"created_at":"Sat Feb 08 15:37:37 +0000 2014","id":432176397474623489,"id_str":"432176397474623489","text":"Skal begynne \u00e5 selge vekter.. Eneste m\u00e5ten det konstant kommer penger i lommen","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":366301747,"id_str":"366301747","name":"skinny-pete","screen_name":"JFarsund","location":"bj\u00f8rge","url":null,"description":"j\u00f8rgen er en tynn gutt med pack.. Men det teller vel ikke? Det gj\u00f8r vel ikke bio heller","protected":false,"followers_count":427,"friends_count":291,"listed_count":2,"created_at":"Thu Sep 01 23:03:49 +0000 2011","favourites_count":5103,"utc_offset":3600,"time_zone":"Copenhagen","geo_enabled":true,"verified":false,"statuses_count":8827,"lang":"no","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/366301747\/1391822743","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":{"type":"Point","coordinates":[60.33700829,5.24626808]},"coordinates":{"type":"Point","coordinates":[5.24626808,60.33700829]},"place":{"id":"2260fcb4a77f2bad","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/2260fcb4a77f2bad.json","place_type":"city","name":"Bergen","full_name":"Bergen, Hordaland","country_code":"NO","country":"Norge","contained_within":[],"bounding_box":{"type":"Polygon","coordinates":[[[5.1602697,60.1848543],[5.1602697,60.5335445],[5.6866852,60.5335445],[5.6866852,60.1848543]]]},"attributes":{}},"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"no"}

这真是一个长期的眼睛疼痛。

我为下一个添加了一些“......”,使其更具可读性,只显示我感兴趣的值:

{…,"user":{"id":366301747,"id_str":"366301747","name":"skinny-pete","screen_name":"JFarsund","location":"bj\u00f8rge","url":null,"description":"j\u00f8rgen er en tynn gutt med pack.. Men det teller vel ikke? Det gj\u00f8r vel ikke bio heller","protected":false,"followers_count":427,"friends_count":291,"listed_count":2,"created_at":"Thu Sep 01 23:03:49 +0000 2011","favourites_count":5103,"utc_offset":3600,"time_zone":"Copenhagen","geo_enabled":true,"verified":false,"statuses_count":8827,"lang":"no","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/366301747\/1391822743","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null}, …}

是的,还在我身边吗?

正如我上面提到的,我想要的是用户ID,我希望将其分配给Tweet对象中的变量“user”。

{…,"user":{"id":366301747,"id_str":"366301747",… }…}

我想要的只是将数字 366301747 分配给我的推文对象中的变量“user”。

但对于我的生活,我似乎无法做到。

为了确保Tweet对象获取它想要的信息而不是它没有的信息,我使用的是Jackson对象:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

所以我的问题。

如何告诉Tweet从我的json字符串中取出 366301747 号码并将其分配给变量“user”?

我更喜欢单独使用Jackson,而不必输入超过必要数量的JAR。

请原谅文字墙。

2 个答案:

答案 0 :(得分:1)

使用Gson库可以非常简单。

因为,您已经完成了创建pojo的艰苦工作,通过查看json您可以验证User是有效的json对象而不是String 1}}值。

因此,让我们稍微修改你的pojo(Tweetuser属性:

User user;

public User getUser() {
    return user;
}

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

User自定义类的位置:

public class User {
    private String id;

    public String getId() {
        return id;
    }

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

现在只需调用您的Gson方法将您的对象转换为json(我将json存储到文件并通过BufferedReader读取):

public static void main(String[] args) throws FileNotFoundException {
        Gson gson = new Gson();
        BufferedReader br = new BufferedReader(new FileReader(
                "json.txt"));

        Tweet tweetObj = gson.fromJson(br, Tweet.class);
        System.out.println(tweetObj.getUser().getId());
    }

输出:

366301747

编辑:根据评论,解决方案使用jackson - 2选项

  1. 保留新创建的User类,ObjectMapper代码保持完全相同,System.out.println(tweet.getUser().getId())为您提供用户ID。
  2. 如果不使用User课程,请将Tweet更改为如下所示:
  3. 代码:

    public class Tweet {
    
        String created_at;
        String id;
        String text;
        Map<String, String> user;
    
        public String getCreated_at() {
            return created_at;
        }
    
        public void setCreated_at(String created_at) {
            this.created_at = created_at;
        }
    
        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 Map<String, String> getUser() {
            return user;
        }
    
        public void setUser(Map<String, String> user) {
            this.user = user;
        }
    
    }
    

    并将调用方法中的userid打印为:

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    
    Tweet tweet = mapper.readValue(br, Tweet.class);
    
    System.out.println(tweet.getUser().get("id"));
    

    获取你:

    366301747
    

答案 1 :(得分:1)

您可以更改setUser方法以获取Map并手动设置user.id

public void setUser(Map<String, Object> props) {
    user = props.get("id").toString();
}