开发者

iphone json twitter web service question

Im using json to load a table in my app from a twitter web service, it works fine when using the search function,

http://search.twitter.com/search.json?q=mobtuts&rpp=5

 the type of json response for the first one is: tenga: {"completed_in" = "0.076"; ...

but when I use the statuses function,

[NSURL URLWithString:@"http://twitter.com/statuses/user_timeline.json?id=hypercomputing"]];

 the type of json response for the second one is: tenga: (
    {
    contributors = "<null>";
    coordinates = "<null>";
    "created_at" = "Thu Aug 04 23:26:05 +0000 2011";...

the result from json is different, so my app doesnt see the second call as a dictionary once imported from json, it sees it as a string, [because of the "(" ??]

here the code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
responseData = [[NSMutableData data] retain];
tweets = [NSMutableArray array];
NSURLRequest *re开发者_如何学Goquest = [NSURLRequest requestWithURL:
//               [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline.json?id=hypercomputing"]]; 
[NSURL URLWithString:@"http://search.twitter.com/search.json?q=mobtuts&rpp=5"]];// hypercomputing
//[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json?id=hypercomputing"]]; 
[[NSURLConnection alloc] initWithRequest:request delegate:self];
    return YES;
}
  #pragma mark NSURLConnection delegate methods
 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
      }
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
     }

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  }



   - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding];
[responseData release];

    NSLog(@"string: %@",responseString);

NSDictionary *results = [responseString JSONValue];

NSLog(@"tenga: %@",results);
//NSLog(@"tenga: %@",[results objectForKey:@"("] );



//NSArray * keys = [results allKeys]; //ensa


//NSLog(@"keys: %@",keys); //ensayo


NSArray *allTweets = [results objectForKey:@"results"];

//NSArray *allTweets = [results objectForKey:@"user"];

NSLog(@"user is: %@",allTweets);


//[viewController setTweets:allTweets];
[window addSubview:viewController.view];
[window makeKeyAndVisible];

 }

so how can I make sure to receive a dictionary from the json call?,

thanks a lot!


Instead of expecting a dictionary every time, considering testing the class of the incoming JSON object and writing code that handles each case.

Or, more to your point, write an intermediary web service to massage the JSON into something your app can recognize.

Since you're working with defined APIs offered by Twitter, you'll have to read up on the documentation to know what to expect and adjust your implementation accordingly. This isn't a case where you can change the data to meet your needs, you have to change the handling of the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜