2009-12-05 5 views
1

iPhone 프로젝트에 Matt Gemmell의 놀라운 MGTwitterEngine을 사용하고 있으며 Obj-C 및 JSON 구문 분석에 익숙하지 않아 간단한 작업이 다소 어려워 보입니다.목표 C에서 MGTwitterEngine으로 검색 결과 구문 분석

나는 this link을 읽었으며 데이터 구조화 방법을 이해하는 데 도움이되지만 여전히 구문 분석 할 수없는 것처럼 보입니다. 내가 그렇게 좋아 getSearchResultsForQuery의 출력의 NSLog를 할 경우

:

NSLog(@"getSearchResultsForQuery: connectionIdentifier = %@", [myTwitterEngine getSearchResultsForQuery:@"#joke" sinceID:0 startingAtPage:1 count:5]); 

나는 콘솔이 얻을 :

2009-12-05 00:14:46.518 Entendu[90565:207] getSearchResultsForQuery: connectionIdentifier = DED541CB-A3DA-4C04-8B23-ED20D13B147E 
2009-12-05 00:14:46.843 Entendu[90565:207] Request succeeded for connectionIdentifier = DED541CB-A3DA-4C04-8B23-ED20D13B147E 
2009-12-05 00:14:46.884 Entendu[90565:207] Got search results for DED541CB-A3DA-4C04-8B23-ED20D13B147E: 
(
     { 
     "created_at" = 1259989857; 
     "from_user" = royanramadhan; 
     "from_user_id" = 64716299; 
     geo = <null>; 
     id = 6361369664; 
     "iso_language_code" = sv; 
     "profile_image_url" = "http://a3.twimg.com/profile_images/455943541/Dup_01_Foto006_normal.jpg"; 
     source = "<a href=\"http://m.twitter.com/\" rel=\"nofollow\">mobile web</a>"; 
     "source_api_request_type" = 37; 
     text = "kenapa di keybord komputer tulisnya \"ENTER\" ? #joke"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259988269; 
     "from_user" = alivenkickin; 
     "from_user_id" = 2547330; 
     geo = <null>; 
     id = 6360783744; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a3.twimg.com/profile_images/65068323/calvin_normal.jpg"; 
     source = "<a href=\"http://www.tweetdeck.com/\" rel=\"nofollow\">TweetDeck</a>"; 
     "source_api_request_type" = 37; 
     text = "RT @andycrash: A nice, calm and respectable lady went into the pharmacy...#joke http://bit.ly/BGDfJ"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259988217; 
     "from_user" = lilbytes; 
     "from_user_id" = 18943691; 
     geo = <null>; 
     id = 6360764474; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a3.twimg.com/profile_images/477338701/twitterProfilePhoto_normal.jpg"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "RT @Pyeman: LOL - Great post RT @cahlan: RT @jac_: Christmas light fun: http://bit.ly/828q7K #lol #funny #joke #christmas"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259987890; 
     "from_user" = jscognam; 
     "from_user_id" = 41472147; 
     geo = <null>; 
     id = 6360648482; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a3.twimg.com/profile_images/432856397/AJ_normal.png"; 
     source = "<a href=\"http://www.tweetdeck.com/\" rel=\"nofollow\">TweetDeck</a>"; 
     "source_api_request_type" = 37; 
     text = "RT @jokestweet: Give me ambiguity or give me something else. #joke"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259987587; 
     "from_user" = jokestweet; 
     "from_user_id" = 54008906; 
     geo = <null>; 
     id = 6360534281; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a1.twimg.com/profile_images/403925746/jokes_twitter_normal.jpg"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "Give me ambiguity or give me something else. #joke"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259987426; 
     "from_user" = RickyRicardoABN; 
     "from_user_id" = 79758746; 
     geo = <null>; 
     id = 6360472740; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a3.twimg.com/profile_images/554599647/l_92120d47e5744fee9da32c4ac71b9eee_normal.jpg"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "#Why do people tell you when they are speechless? #joke"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259984750; 
     "from_user" = socialEspeaking; 
     "from_user_id" = 80950838; 
     geo = <null>; 
     id = 6359413973; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a1.twimg.com/profile_images/542502866/jack_sm_normal.jpg"; 
     source = "<a href=\"http://apiwiki.twitter.com/\" rel=\"nofollow\">API</a>"; 
     "source_api_request_type" = 37; 
     text = "RT @andycrash A nice, calm and respectable lady went into the pharmacy...#joke http://x4.lol.0sp.in o_O"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259984140; 
     "from_user" = oceanbutterfly7; 
     "from_user_id" = 70740820; 
     geo = <null>; 
     id = 6359164521; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a1.twimg.com/profile_images/558958306/Tiger-puppy_love_normal.jpg"; 
     source = "<a href=\"/devices\" rel=\"nofollow\">txt</a>"; 
     "source_api_request_type" = 37; 
     text = "@jokestweet: \" Hire a teenage while they still know everything,\" #joke"; 
     "to_user" = jokestweet; 
     "to_user_id" = 54008906; 
    }, 
     { 
     "created_at" = 1259983842; 
     "from_user" = "Fab_DTY"; 
     "from_user_id" = 51115644; 
     geo = <null>; 
     id = 6359037805; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a3.twimg.com/profile_images/515204077/moto_0326_normal.jpeg"; 
     source = "<a href=\"http://ubertwitter.com\" rel=\"nofollow\">UberTwitter</a>"; 
     "source_api_request_type" = 37; 
     text = "RT @nutshellcards: Energizer Bunny arrested, charged with battery. lmao #Joke"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259983640; 
     "from_user" = chunyip; 
     "from_user_id" = 1221169; 
     geo = <null>; 
     id = 6358953626; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://s.twimg.com/a/1259808911/images/default_profile_6_normal.png"; 
     source = "<a href=\"http://mobileways.de/gravity\" rel=\"nofollow\">Gravity</a>"; 
     "source_api_request_type" = 37; 
     text = "RT @jokestweet: If electricity comes from electrons, does that mean that morality comes from morons? #joke"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259983356; 
     "from_user" = nutshellcards; 
     "from_user_id" = 54486591; 
     geo = <null>; 
     id = 6358837191; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a3.twimg.com/profile_images/515618469/blue_normal.png"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "Energizer Bunny arrested, charged with battery. lmao\n#Joke"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259983222; 
     "from_user" = sagewhistler; 
     "from_user_id" = 3737203; 
     geo = <null>; 
     id = 6358780332; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a1.twimg.com/profile_images/552759084/cowboycig_normal.GIF"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "#Joke \"What's the shotgun for?\" asks the homeowner. The bear remover replies, \"If the bear knocks me off the roof, shoot the dog.\" [end]"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259983181; 
     "from_user" = sagewhistler; 
     "from_user_id" = 3737203; 
     geo = <null>; 
     id = 6358763080; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a1.twimg.com/profile_images/552759084/cowboycig_normal.GIF"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "#Joke The bear will then be subdued enough for me to put him in the cage in the back of the van.\"He hands the shotgun to the homeowner..."; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259983144; 
     "from_user" = sagewhistler; 
     "from_user_id" = 3737203; 
     geo = <null>; 
     id = 6358748440; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a1.twimg.com/profile_images/552759084/cowboycig_normal.GIF"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "#Joke When the bear falls off, the pit bull is trained to grab his testicles and not let go..."; 
     "to_user_id" = <null>; 
    }, 
     { 
     "created_at" = 1259983112; 
     "from_user" = sagewhistler; 
     "from_user_id" = 3737203; 
     geo = <null>; 
     id = 6358736099; 
     "iso_language_code" = en; 
     "profile_image_url" = "http://a1.twimg.com/profile_images/552759084/cowboycig_normal.GIF"; 
     source = "<a href=\"http://twitter.com/\">web</a>"; 
     "source_api_request_type" = 37; 
     text = "#Joke \"I'm going to put this ladder up against the roof, then I'm going to go up there & knock the bear off the roof with this baseball bat"; 
     "to_user_id" = <null>; 
    }, 
     { 
     "completed_in" = 0; 
     "max_id" = 6361369664; 
     "next_page" = "?page=2&max_id=6361369664&q=%23joke"; 
     page = 1; 
     query = "%23joke"; 
     "refresh_url" = "?since_id=6361369664&q=%23joke"; 
     "results_per_page" = 15; 
     "since_id" = 0; 
     "source_api_request_type" = 37; 
    } 
) 

이상한 것은 내가 계산이있다 : 5, 아직 I 더 많은 결과를 얻고 있습니다.

실제 구문 분석을 위해 출력을 NSArray에 저장 한 다음 valueForKey를 사용하여 쿼리 할 수있는 NSDictionary (예 : @ "text")를 저장하겠습니까?

두 개의 대리자 메서드가 너무 (requestSucceeded : 및 searchResultsReceived : forRequest :) 호출되고 있지만이 파싱에 어떻게 영향을 미치는지 잘 모르겠다.

searchResultsReceived의 실제 위임 방법 forRequest는 :이다

- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier 
{ 
NSLog(@"Got search results for %@:\r%@", connectionIdentifier, searchResults); 
} 

이 결과는 실제의 SearchResult 변수가 저장되는 것을 의미한다.

그래도 개별 문자열을 얻으려면 어떻게해야합니까?

도움 주셔서 감사합니다.

답변

2

위임자의 searchResults 변수는 사전 배열입니다. 해당 배열에 색인을 작성한 다음 사전에있는 키의 값을 잡기 만하면됩니다. 예 :

- (void)searchResultsReceived:(NSArray *)searchResults 
        forRequest:(NSString *)connectionIdentifier 
{ 
    if ([searchResults count] > 0) 
    { 
     NSDictionary *result = [searchResults objectAtIndex:0]; 

     NSString *fromUser = [result valueForKey:@"from_user"]; 
     NSString *fromUserID = [result [email protected]"from_user_id"]; 
     // ... 
     NSString *text = [result [email protected]"text"]; 

     NSLog(@"User %@(%@): %@", fromUser, fromUserID, text); 
    } 
} 
+0

고마워요! 이것은 내가 계속해서 데이터를 연구 할 수있는 방법을 제공합니다. 델리게이트 함수의 범위를 벗어난 개별 문자열에 액세스하려면 반환 형식을 Array로 설정하고 필요한 정보 만 반환 할 수 있습니까? 아니면 원래 배열 자체를 반환하여 추출을 수행 할 수 있습니까? 다시 고맙습니다. – Parimal

+0

NSArray 인스턴스 변수 (ivar) 속성을 만들고 그 위에 고정시킬 수 있습니다 (예 : [self setItems : searchResults]; 이것은 인스턴스 변수가 @property (assign) NSArray * items와 같은 속성으로 선언 될 것을 요구합니다; .h를 입력 한 다음 .m의 항목을 @synthesize하십시오. –

+0

오, 훌륭합니다! 도와 주셔서 정말 감사합니다! – Parimal

1

당신이해야 할 일을 파싱해야할지 모르겠습니다. 당신은 엔진에 델리게이트 (콜백 같은 것이지만 실제 객체)를 제공해야하는데, 엔진은 결과가있을 때이를 알리기 위해 사용할 것입니다. 결과는 NSArrayNSDictionary으로 되돌아 와야합니다. 즉, 구문 분석을 전혀 할 필요가 없음을 의미합니다.

+0

아, 지금 당장! 감사. – Parimal