【问题标题】:Parse Raw Json Strings解析原始 Json 字符串
【发布时间】:2016-11-01 19:38:33
【问题描述】:

我使用 Twitter4j 抓取了数百万条推文,但令我惊讶的是,所有推文都以原始 json 格式存储。以下是格式化行的示例:

StatusJSONImpl{
  createdAt=TueNov0119: 00: 04CET2016,
  id=793512948027326464,
  text='RT @DylanYamaha_: Et profitez vraiment des personnes qui sont près de vous, parce que sa arrive très très vite un malheur..',
  source='<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
  isTruncated=false,
  inReplyToStatusId=-1,
  inReplyToUserId=-1,
  isFavorited=false,
  isRetweeted=false,
  favoriteCount=0,
  inReplyToScreenName='null',
  geoLocation=null,
  place=null,
  retweetCount=0,
  isPossiblySensitive=false,
  lang='fr',
  contributorsIDs=[

  ],
  retweetedStatus=StatusJSONImpl{
    createdAt=TueNov0118: 38: 05CET2016,
    id=793507418244313088,
    text='Et profitez vraiment des personnes qui sont près de vous, parce que sa arrive très très vite un malheur..',
    source='<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
    isTruncated=false,
    inReplyToStatusId=-1,
    inReplyToUserId=-1,
    isFavorited=false,
    isRetweeted=false,
    favoriteCount=56,
    inReplyToScreenName='null',
    geoLocation=null,
    place=null,
    retweetCount=175,
    isPossiblySensitive=false,
    lang='fr',
    contributorsIDs=[

    ],
    retweetedStatus=null,
    userMentionEntities=[

    ],
    urlEntities=[

    ],
    hashtagEntities=[

    ],
    mediaEntities=[

    ],
    symbolEntities=[

    ],
    currentUserRetweetId=-1,
    user=UserJSONImpl{
      id=2242998313,
      name='_',
      screenName='DylanYamaha_',
      location='Marseille, France',
      description='null',
      isContributorsEnabled=false,
      profileImageUrl='http://pbs.twimg.com/profile_images/793110090727424002/9bLOivem_normal.jpg',
      profileImageUrlHttps='https://pbs.twimg.com/profile_images/793110090727424002/9bLOivem_normal.jpg',
      isDefaultProfileImage=false,
      url='null',
      isProtected=false,
      followersCount=12357,
      status=null,
      profileBackgroundColor='ABB8C2',
      profileTextColor='333333',
      profileLinkColor='89C9FA',
      profileSidebarFillColor='DDEEF6',
      profileSidebarBorderColor='FFFFFF',
      profileUseBackgroundImage=false,
      isDefaultProfile=false,
      showAllInlineMedia=false,
      friendsCount=87,
      createdAt=ThuDec1223: 15: 28CET2013,
      favouritesCount=6007,
      utcOffset=3600,
      timeZone='Amsterdam',
      profileBackgroundImageUrl='http://abs.twimg.com/images/themes/theme1/bg.png',
      profileBackgroundImageUrlHttps='https://abs.twimg.com/images/themes/theme1/bg.png',
      profileBackgroundTiled=false,
      lang='fr',
      statusesCount=2049,
      isGeoEnabled=false,
      isVerified=false,
      translator=false,
      listedCount=43,
      isFollowRequestSent=false,
      withheldInCountries=null
    },
    withHeldInCountries=null,
    quotedStatusId=-1,
    quotedStatus=null
  },
  userMentionEntities=[
    UserMentionEntityJSONImpl{
      name='_',
      screenName='DylanYamaha_',
      id=2242998313
    }
  ],
  urlEntities=[

  ],
  hashtagEntities=[

  ],
  mediaEntities=[

  ],
  symbolEntities=[

  ],
  currentUserRetweetId=-1,
  user=UserJSONImpl{
    id=393519159,
    name='Tiphaine.',
    screenName='LehmannTiphaine',
    location='France',
    description='Snapchat : tiphainelehmann | IG : tiphainelmn',
    isContributorsEnabled=false,
    profileImageUrl='http://pbs.twimg.com/profile_images/777174096958332928/yoz2aPp2_normal.jpg',
    profileImageUrlHttps='https://pbs.twimg.com/profile_images/777174096958332928/yoz2aPp2_normal.jpg',
    isDefaultProfileImage=false,
    url='null',
    isProtected=false,
    followersCount=145,
    status=null,
    profileBackgroundColor='000000',
    profileTextColor='333333',
    profileLinkColor='000000',
    profileSidebarFillColor='F3F3F3',
    profileSidebarBorderColor='000000',
    profileUseBackgroundImage=true,
    isDefaultProfile=false,
    showAllInlineMedia=false,
    friendsCount=200,
    createdAt=TueOct1819: 17: 04CEST2011,
    favouritesCount=3202,
    utcOffset=3600,
    timeZone='Paris',
    profileBackgroundImageUrl='http://pbs.twimg.com/profile_background_images/753348262/9d241c29a193586d5dc519838bded4c9.jpeg',
    profileBackgroundImageUrlHttps='https://pbs.twimg.com/profile_background_images/753348262/9d241c29a193586d5dc519838bded4c9.jpeg',
    profileBackgroundTiled=true,
    lang='fr',
    statusesCount=3462,
    isGeoEnabled=true,
    isVerified=false,
    translator=false,
    listedCount=3,
    isFollowRequestSent=false,
    withheldInCountries=null
  },
  withHeldInCountries=null,
  quotedStatusId=-1,
  quotedStatus=null
  }

我在解析这些 JSON 字符串时遇到了很多问题。第一个问题是一些推文不止一行。我通过连接同一条推文的行来解决这个问题。然后我必须用 "" 替换所有 StatusJSONImplUserJSONImploccurrences 以使用 JSONObject 构造函数。 主要问题是如何获取所有推文属性。我用过:

JSONObject jsonObj = new JSONObject(jsonline); //from twitter4j.JSONObject;

但每当推文文本包含“'”之类的字符时,我就有:

twitter4j.JSONException:在 117 [第 118 行第 1 行字符] 处应为“,”或“}”

即使文本不包含“'”,我仍然无法提取推文的时间创建(createdAt)。所以我创建了一个字符串:

String statusFromRaw = "{\"filter_level\": \"low\",";
    statusFromRaw+= "\"retweeted\":"+jsonObj.get("isRetweeted")+",";
.....

为了重新创建更清洁的状态,并使用:

Status status = TwitterObjectFactory.createStatus(statusFromRaw);

但在大多数情况下,我在解析许多为空的属性时仍然存在问题。 有什么建议吗?

【问题讨论】:

  • StatusJSONImpl 不是原始 JSON,您需要先将状态对象转换为带有 TwitterObjectFactory.getRawJSON(status); 的 json
  • 我已经尝试过了,但我遇到了这个异常:Exception in thread "main" java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true. 将其设置为 true 为时已晚,因为它应该只执行一次,当调用 TwitterObjectFactory 时。
  • 我觉得应该用正则表达式来一一提取属性...

标签: java json parsing twitter4j tweets


【解决方案1】:

不要使用 Twitter4j。只需自己点击 Twitter 的 API 并使用 Jackson 将其映射到对象。

您不应该试图围绕 IMO 框架解决问题。

【讨论】:

  • 知道了,但问题是我已经有数百万条这种格式的推文了。我现在应该处理这些推文。
猜你喜欢
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多