2011-12-18 55 views
1

我有一个复杂的JSON写在一个字符串。我知道java和一点mysql。我需要从json中创建一个数据库。如何解析json到数据库?

我正在使用一些Twitter数据,因此推文包含推文推文的用户的描述,并且在推特被转推的情况下,它包含推介此用户之前用户的描述。

我的目标是创建一个用户表(或数组或任何其他数据结构),其中包含此用户发送的所有推文以及所有推文转发的推文。

tweet对象包含大约50-80个对象,所以在这里给出一个例子将使这篇文章真的很长。

StatusJSONImpl{createdAt=Wed Sep 28 12:04:55 IST 2011, id=118936707830775808, text='RT @nytimesbits: Google's Biggest Threat Is Google http://t.co/kTNqJFJC', source='web', isTruncated=false, inReplyToStatusId=-1, inReplyToUserId=-1, isFavorited=false, inReplyToScreenName='null', geoLocation=null, place=null, retweetCount=6, wasRetweetedByMe=false, contributors=null, annotations=null, retweetedStatus=StatusJSONImpl{createdAt=Wed Sep 28 05:35:26 IST 2011, id=118838689248985088, text='Google's Biggest Threat Is Google http://t.co/kTNqJFJC', source='<a href="http://www.nytimes.com/twitter" rel="nofollow">The New York Times</a>', isTruncated=false, inReplyToStatusId=-1, inReplyToUserId=-1, isFavorited=false, inReplyToScreenName='null', geoLocation=null, place=null, retweetCount=6, wasRetweetedByMe=false, contributors=null, annotations=null, retweetedStatus=null, userMentionEntities=[], urlEntities=[URLEntityJSONImpl{start=34, end=54, url=http://t.co/kTNqJFJC, expandedURL=http://nyti.ms/pR9DfX, displayURL=nyti.ms/pR9DfX}], hashtagEntities=[], user=UserJSONImpl{id=14434070, name='NYTimes Bits Blog', screenName='nytimesbits', location='The Cloud', description='News and analysis on tech and business. Also here: select retweets from NYT tech writers and friends. Account maintained by David F. Gallagher (@davidfg).', isContributorsEnabled=true, profileImageUrl='http://a1.twimg.com/profile_images/108833947/bits75_normal.jpg', profileImageUrlHttps='https://si0.twimg.com/profile_images/108833947/bits75_normal.jpg', url='http://nytimes.com/bits', isProtected=false, followersCount=53180, status=null, profileBackgroundColor='9ae4e8', profileTextColor='000000', profileLinkColor='0000ff', profileSidebarFillColor='e0ff92', profileSidebarBorderColor='87bc44', profileUseBackgroundImage=true, showAllInlineMedia=false, friendsCount=139, createdAt=Fri Apr 18 20:49:26 IST 2008, favouritesCount=5, utcOffset=-18000, timeZone='Eastern Time (US & Canada)', profileBackgroundImageUrl='http://a3.twimg.com/profile_background_images/4780380/twitter_post.png', profileBackgroundImageUrlHttps='https://si0.twimg.com/profile_background_images/4780380/twitter_post.png', profileBackgroundTiled=true, lang='en', statusesCount=6360, isGeoEnabled=false, isVerified=true, translator=false, listedCount=4671, isFollowRequestSent=false}}, userMentionEntities=[UserMentionEntityJSONImpl{start=3, end=15, name='NYTimes Bits Blog', screenName='nytimesbits', id=14434070}], urlEntities=[URLEntityJSONImpl{start=51, end=71, url=http://t.co/kTNqJFJC, expandedURL=http://nyti.ms/pR9DfX, displayURL=nyti.ms/pR9DfX}], hashtagEntities=[], user=UserJSONImpl{id=17989546, name='Wolfgang Fasching-K.', screenName='wwwof', location='Vienna', description='Digital ist besser. Fokus: IT & Internet, World News & US Politik, Medien & Pop/Kultur. http://www.riverone.at', isContributorsEnabled=false, profileImageUrl='http://a0.twimg.com/profile_images/67758989/SF050069-w_normal.JPG', profileImageUrlHttps='https://si0.twimg.com/profile_images/67758989/SF050069-w_normal.JPG', url='null', isProtected=false, followersCount=59, status=null, profileBackgroundColor='C0DEED', profileTextColor='333333', profileLinkColor='0084B4', profileSidebarFillColor='DDEEF6', profileSidebarBorderColor='C0DEED', profileUseBackgroundImage=true, showAllInlineMedia=false, friendsCount=64, createdAt=Tue Dec 09 17:09:35 IST 2008, favouritesCount=0, utcOffset=3600, timeZone='Vienna', profileBackgroundImageUrl='http://a3.twimg.com/profile_background_images/234523169/Naschmarkt-Wien-Juni10-2010-s.jpg', profileBackgroundImageUrlHttps='https://si0.twimg.com/profile_background_images/234523169/Naschmarkt-Wien-Juni10-2010-s.jpg', profileBackgroundTiled=true, lang='en', statusesCount=269, isGeoEnabled=false, isVerified=false, translator=false, listedCount=4, isFollowRequestSent=false}} 
+1

给出至少一个提取物! – fge 2011-12-18 11:25:22

+0

这实际上是2个问题之一 - 如何解析JSON以及如何在数据库中保存数据。哪一部分很难? – extraneon 2011-12-18 11:35:36

+0

增加了一个例子。 – R1234 2011-12-18 11:38:34

回答

1

对于JSON解析,我建议Jackson。另外,为了验证你的输入,你应该看看JSON模式(如果你愿意的话,我有一个实现)。

这里是如何使用杰克逊的字符串解析JSON:

final ObjectMapper mapper = new ObjectMapper(); 

final JsonNode node = mapper.readTree(yourInput); 

// Access members: 

node.get(0); // access node 0 of an array 

for (final JsonNode entry: node) { 
    ... // cycle through array nodes 
} 

node.get("foo"); // access property "foo" of an object 

node.get("foo").getTextValue(); // access as a text 

// etc etc 

它也有一个选项S *负荷序列化的POJO如果这就是你想要的。

1

您的第一步是解析JSON以获取对象图,使用类似gson或其他任何数据库的库。然后(这看起来很普遍,但它是一个相当开放的问题),这是确定模式应该是什么,创建表,并循环遍历填充它们的对象图的问题。

如果您被允许,您可以查看“文档数据库”(所谓的NoSQL)而不是SQL,因为它们通常允许模式更加流畅。

+0

+1将问题分解为两个组成部分(JSON解析和持久化)。我并不完全同意NoSQL的评论 - 这一切都取决于数据库中数据的使用情况。 – extraneon 2011-12-18 11:34:40

+0

@ extraneon:是的,完全取决于,OP应该使用SQL RDBMS,如果这更适合这个问题。总是选择适合这个工作的工具。:-) – 2011-12-18 11:50:38

0

如果你的问题只是Twitter,你可以寻找像Twiiter4J或Spring Social这样的专用API,它应该为推文提供准备好的java bean。 如果你正在实现一个小项目Gson是解析的最佳解决方案。但是,如果你使一些更复杂的东西,我建议你使用杰克逊解析和Hibernate作为应用程序和SQL数据库之间的中间件。