从twitter数据转储解析json

时间:2012-11-26 14:42:18

标签: python json twitter

我收到了一些我希望解析的Twitter数据转储,最好是用Python(开头)。我没有创建JSON,我不熟悉StatusJSONImplUserJSONImplUserMentionEntityJSONImpl指令。我该如何解析它?我必须先“清理”它还是json(或类似的模块)弄清楚它?

json.loads(s)与下面的字符串s一起使用,我得到ValueError: No JSON object could be decoded

StatusJSONImpl {
    createdAt = Tue Sep 20 08: 31: 28 CST 2011,
    id = 115946140620435456,
    text = 'RT @williamsanches: 4? Dica: Sonhe sempre. Deus acredita no cora??o que sonha!',
    source = 'web',
    isTruncated = false,
    inReplyToStatusId = -1,
    inReplyToUserId = -1,
    isFavorited = false,
    inReplyToScreenName = 'null',
    geoLocation = null,
    place = null,
    retweetCount = 3,
    wasRetweetedByMe = false,
    contributors = null,
    annotations = null,
    retweetedStatus = StatusJSONImpl {
        createdAt = Tue Sep 20 08: 29: 50 CST 2011,
        id = 115945729465401344,
        text = '4? Dica: Sonhe sempre. Deus acredita no cora??o que sonha!',
        source = 'web',
        isTruncated = false,
        inReplyToStatusId = -1,
        inReplyToUserId = -1,
        isFavorited = false,
        inReplyToScreenName = 'null',
        geoLocation = null,
        place = null,
        retweetCount = 3,
        wasRetweetedByMe = false,
        contributors = null,
        annotations = null,
        retweetedStatus = null,
        userMentionEntities = [],
        urlEntities = [],
        hashtagEntities = [],
        user = UserJSONImpl {
            id = 54728012, name = 'William_Sanches', screenName = 'williamsanches', location = '', description = 'Professor Universitário, escritor e palestrante.', isContributorsEnabled = false, profileImageUrl = 'http://a1.twimg.com/profile_images/1533016174/msn_normal.jpg', profileImageUrlHttps = 'https://si0.twimg.com/profile_images/1533016174/msn_normal.jpg', url = 'http://www.williamsanches.com.br', isProtected = false, followersCount = 10905, status = null, profileBackgroundColor = 'EDECE9', profileTextColor = '634047', profileLinkColor = '088253', profileSidebarFillColor = 'c7c7c7', profileSidebarBorderColor = 'D3D2CF', profileUseBackgroundImage = true, showAllInlineMedia = false, friendsCount = 232, createdAt = Wed Jul 08 07: 56: 17 CST 2009,
            favouritesCount = 1,
            utcOffset = -14400,
            timeZone = 'Santiago',
            profileBackgroundImageUrl = 'http://a3.twimg.com/profile_background_images/125874404/tela-william-sanches.jpg',
            profileBackgroundImageUrlHttps = 'https://si0.twimg.com/profile_background_images/125874404/tela-william-sanches.jpg',
            profileBackgroundTiled = false,
            lang = 'es',
            statusesCount = 7499,
            isGeoEnabled = false,
            isVerified = false,
            translator = false,
            listedCount = 130,
            isFollowRequestSent = false
        }
    },
    userMentionEntities = [UserMentionEntityJSONImpl {
        start = 3, end = 18, name = 'William_Sanches', screenName = 'williamsanches', id = 54728012}],
    urlEntities = [],
    hashtagEntities = [],
    user = UserJSONImpl {
        id = 170132028, name = 'Andrezza Santiago', screenName = 'andrezzasancar', location = '', description = 'Uma pessoa que ama a vida e vive pra ser amada.Ama acima de tudo a DEUS,sua familia e seus amigos! Alguém alegre e de bem com a vida.', isContributorsEnabled = false, profileImageUrl = 'http://a1.twimg.com/profile_images/1505208968/DSC_0414_normal.JPG', profileImageUrlHttps = 'https://si0.twimg.com/profile_images/1505208968/DSC_0414_normal.JPG', url = 'null', isProtected = false, followersCount = 64, status = null, profileBackgroundColor = '642D8B', profileTextColor = '3D1957', profileLinkColor = 'FF0000', profileSidebarFillColor = '7AC3EE', profileSidebarBorderColor = '65B0DA', profileUseBackgroundImage = true, showAllInlineMedia = false, friendsCount = 330, createdAt = Sat Jul 24 09: 02: 05 CST 2010,
        favouritesCount = 0,
        utcOffset = -14400,
        timeZone = 'Santiago',
        profileBackgroundImageUrl = 'http://a0.twimg.com/profile_background_images/321379737/images__2_.jpg',
        profileBackgroundImageUrlHttps = 'https://si0.twimg.com/profile_background_images/321379737/images__2_.jpg',
        profileBackgroundTiled = true,
        lang = 'pt',
        statusesCount = 3129,
        isGeoEnabled = true,
        isVerified = false,
        translator = false,
        listedCount = 20,
        isFollowRequestSent = false
    }
}​

1 个答案:

答案 0 :(得分:0)

假设你有一个格式正确的JSON对象,这可以解决问题:

import json

data = json.loads(json_object)
data.get('ids')

其中'json_object'是Twitter API输出的JSON。 'data'现在是json_object的字典表示。