Java json字符串中的特定信息
我在Eclipse中使用推文已经有一段时间了,这些推文是以json字符串表示的 为此,我创建了一个名为Tweet(original,嗯?)的对象,它从json字符串中获取某些信息,并将其存储在Tweet对象中。没什么特别的 我的Tweet类如下所示:Java json字符串中的特定信息,java,json,twitter,Java,Json,Twitter,我在Eclipse中使用推文已经有一段时间了,这些推文是以json字符串表示的 为此,我创建了一个名为Tweet(original,嗯?)的对象,它从json字符串中获取某些信息,并将其存储在Tweet对象中。没什么特别的 我的Tweet类如下所示: public class Tweet implements TwitterMelding { public Tweet() { } String created_at; String id; String text; String user;
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;
}
}
现在,尽管看起来很简单,但有一个不起作用
特别是字符串用户。它应该做的是存储发布推文的人的用户ID
以下是从Twitter上获得的推文,其长度非常可怕:
{"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分配给Tweet对象中的变量“user”
{…,"user":{"id":366301747,"id_str":"366301747",… }…}
但就我的一生而言,我似乎无法做到这一点
为了确保Tweet对象得到了它想要的信息,而不是它没有得到的信息,我使用了Jackson对象:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
所以我的问题是
我如何告诉Tweet从我的json字符串中获取366301747数字并将其分配给变量“user”
我更喜欢杰克逊一个人做,不必进口太多的罐子
请原谅文本墙。使用库可以非常简单 因为,您已经完成了创建pojo的艰苦工作,通过查看
json
,您可以验证User
是有效的json
对象,而不是String
值
因此,让我们稍微修改一下pojo的(Tweet
)user
属性:
User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
其中,用户
自定义类为:
public class User {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
现在只需调用your Gson方法将对象从json
(我将json
存储到一个文件中,并通过BufferedReader
读取它)转换为:
输出:
366301747
编辑:根据评论,使用jackson-2选项的解决方案
User
类,ObjectMapper代码保持不变,System.out.println(tweet.getUser().getId())
获取用户IDUser
类,请将您的Tweet
更改为如下所示:366301747
您可以将
setUser
方法更改为获取Map
并手动设置user.id
:
public void setUser(Map<String, Object> props) {
user = props.get("id").toString();
}
public void setUser(地图道具){
user=props.get(“id”).toString();
}
我明白你的意思,这似乎是一个可以接受的想法。但是没有类用户,我有什么方法可以做到这一点吗?我的意思是,我可以直接从我的Tweet对象获取用户ID吗?这可能对杰克逊有用吗?如果我有困难,请原谅。先生,你应该得到不止一杯啤酒。我真的很感激,因为我坐了好几个小时没有收到这个。现在可以了。你让我一整天都很开心@ViRALiC很乐意提供帮助:)我对Map有点陌生,那么您是否愿意解释一下我将如何使用上面带有json字符串的setUser方法?在我的场景中,这个方法将使用什么参数?请原谅这个新问题。基本上,当你有setUser(Map props)
方法时,props
将是一个Map{“id”:366301747,“id_str”:“366301747”,“name”:“skinny pete”,…
。然后你可以提取jsonid
字段并将其分配给这个。user
user=props.get(“id”).toString()
正是这样做的。由于您使用的是Jackson,您不需要自己将参数传递给setUser
。当您使用mapper.readValue(…)
时,Jackson会在幕后调用setUser
方法。非常感谢您的解释。您的回答非常有用!
366301747
public void setUser(Map<String, Object> props) {
user = props.get("id").toString();
}