【问题标题】:iphone json twitter web service questioniphone json twitter 网络服务问题
【发布时间】:2011-08-30 14:02:58
【问题描述】:

我正在使用 json 从 twitter 网络服务加载我的应用程序中的表, 使用搜索功能时效果很好,

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"; ...

但是当我使用状态功能时,

[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";...

json 的结果不同,所以我的应用程序在从 json 导入后不会将第二次调用视为字典,而是将其视为字符串,[因为 "(" ??]

这里是代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
responseData = [[NSMutableData data] retain];
tweets = [NSMutableArray array];
NSURLRequest *request = [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];

 }

那么我怎样才能确保从 json 调用中接收到字典呢?,

非常感谢!

【问题讨论】:

    标签: ios json twitter dictionary


    【解决方案1】:

    考虑测试传入 JSON 对象的类并编写处理每种情况的代码,而不是每次都期待字典。

    或者,更重要的是,编写一个中间 Web 服务来将 JSON 转换为您的应用可以识别的内容。

    由于您使用的是 Twitter 提供的已定义 API,因此您必须阅读文档以了解预期内容并相应地调整您的实现。这不是您可以更改数据以满足您的需求的情况,您必须更改数据的处理方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多