2011-08-19 32 views
1

我想在我的iPhone应用程序中从此URL拆分json部分。想要从URL中拆分JSON而不是响应

http://vkontakte.ru/login_success.html#session={"expire":"1272008089","mid":"100172","secret":"9c8d8f0305","sid":"1131703552161ae352a1256402e3140d7cbde41b1602a93d15472c82 “}

我尝试和并保存了JSON成的NSString,但我所得到的是

http://vkontakte.ru/api/login_success.html?session=%7B%22mid%22 :113158415,%22secret%22:%227ce58bfcd3%22,%22sid%22:%2203831b43c1bb992f9477efbfe96e83f6ecff1c1b661315ac0a20719cf57a44%22,%22expire%22:0%7D

这不是JSOn格式。以下是我的代码

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 

    NSURL *url = [request URL]; 
    NSError* error; 
    NSLog (@"json path %@",url); 
    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
    NSURLRequest *request1 = [NSURLRequest requestWithURL:url]; 
    NSString *json_string = [url absoluteString]; 
    NSArray *arr = [json_string componentsSeparatedByString:@"="]; 
    json_string = [arr objectAtIndex:1]; 
     // parse the JSON response into an object 
     // Here we're using NSArray since we're parsing an array of JSON status objects 
    NSArray *statuses = [json_string JSONValue]; 

    NSLog(@"%@",error); 

     // Each element in statuses is a single status 
     // represented as a NSDictionary 
     for (NSDictionary *status in statuses) 
     { 
      // You can retrieve individual values using objectForKey on the status NSDictionary 
      // This will print the tweet and username to the console 
      NSLog(@"%@ - %@", [status objectForKey:@"secret "], [[status objectForKey:@"mid"] objectForKey:@"sid"]); 
     } 
     return YES; 
} 

如何进一步移动?

回答

1

尝试更换:

NSString *json_string = [url absoluteString];

NSString *json_string = [[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];。尽管如此,这真是一件很奇怪的事情。为什么网址中有JSON?

+0

我想在Vkontakte分享他们给这个URL的密钥 – Pras

+0

非常感谢。它效果很好! – Pras