gpt4 book ai didi

java - 如何将json解析到数据库?

转载 作者:搜寻专家 更新时间:2023-10-30 23:19:22 25 4
gpt4 key购买 nike

我有一个用字符串编写的复杂 json。我知道 java 和一点 mysql。我需要用 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}}

最佳答案

对于JSON解析,我推荐Jackson .此外,为了验证您的输入,您应该查看 JSON Schema(如果需要,我有一个实现)。

以下是如何使用 Jackson 解析字符串中的 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

如果您需要的话,它还有大量选项可以序列化为 POJO。

关于java - 如何将json解析到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8551355/

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