2016-11-01 4 views
0

나는 Twitter4j를 사용하여 수백만 개의 트윗을 크롤링했지만, 놀랍게도 모든 트윗은 원시 json 형식으로 저장됩니다. 다음은 형식이 지정된 행의 예입니다.Raw Json String을 파싱합니다.

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 문자열을 구문 분석하는 데 많은 문제가있었습니다. 첫 번째 문제는 일부 트윗이 둘 이상의 줄에 나옵니다. 나는 같은 짹짹의 라인을 연결하여 이것을 해결했다. 그런 다음 StatusJSONImplUserJSONImpl 건을 ""로 바꾸어 JSONObject 생성자를 사용해야합니다. 주요 쟁점은 모든 트윗 속성을 얻는 방법입니다. 내가 사용 : 117에서 '}'또는 '[문자 118 라인

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

을하지만, 트윗 문구는 "'":

twitter4j.JSONException 예상을, 내가 가진 '와 같은 문자를 포함 할 때마다 1]

텍스트에 '' '가 포함되지 않은 경우에도 여전히 트윗의 시간 생성 (createdAt)을 추출 할 수 없습니다. 깨끗한 상태를 재 작성하고 사용하기 위해

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

: 그래서 문자열을 생성

Status status = TwitterObjectFactory.createStatus(statusFromRaw); 

을하지만 난 여전히 대부분의 경우는 null 많은 속성을 구문 분석에 문제가있다. 제안 사항이 있습니까?

+0

'StatusJSONImpl'은 원시 JSON이 아니며 먼저 상태 객체를'TwitterObjectFactory.getRawJSON (status); '으로 변환해야합니다. – FeanDoe

+0

이미 시도해 보았습니다.'예외가 있음 ' "main"java.lang.IllegalStateException : jsonStoreEnabled가 true로 설정되지 않은 것 같습니다. TwitterObjectFactory가 호출 될 때 한 번만 수행해야하기 때문에 true로 설정하기에는 너무 늦었습니다. – Bill

+0

나는 정규 표현식을 사용하여 하나씩 속성을 추출해야한다고 생각합니다 ... – Bill

답변

0

Twitter4j를 사용하지 마십시오. Twitter의 API를 직접 누르고 Jackson을 사용하여 객체에 매핑하십시오.

프레임 워크 IMO를 해결하려고해서는 안됩니다.

+0

그래도 문제는 이미 수백만 개의 트윗을 해당 형식으로 가지고 있다는 것입니다. 나는이 트윗을 지금 다루어야한다. – Bill

관련 문제