2011-05-11 193 views
0

當解析json的服務,它工作正常第一次,但在第二次它給了我下面錯誤解析JSON

-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x5f57450 {NSLocalizedDescription=Unexpected end of string}" 

由於給出的錯誤提前

代碼開始分析:

NSString *urlString = [NSString stringWithFormat:@"http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=%@",tt.text]; 

解析代碼::

- (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 { 
    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"error" message:@"Problem with parsing data" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO; 
    self.responseData = nil; 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    self.responseData = nil; 
    NSLog(@"%@",[responseString JSONValue]); 
    NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"results"]; 
    [responseString release]; 
    NSLog(@"count === %d",[latestLoans count]); 
     for (int i = 0; i < [latestLoans count]; i++) 
     { 
      NSLog(@" inside for loop %@",[latestLoans objectAtIndex:i]); 
      NSDictionary* loan = [latestLoans objectAtIndex:i]; 
      NSLog(@" trackanme=== %@",[loan objectForKey:@"collectionName"]); 
       NSLog(@" artworkurl=== %@",[loan objectForKey:@"artworkUrl60"]); 
      [titlearray addObject:[loan objectForKey:@"collectionName"]]; 
      [imagelink addObject:[loan objectForKey:@"artworkUrl60"]]; 

     } 
    //get latest loan 

    NSLog(@"titlearray count == %d",[titlearray count]); 
    NSLog(@"imagelink count ===%d",[imagelink count]); 
    //fetch the data 
    //NSString* fundedAmount = [loan objectForKey:@"Kind"]; 
    //NSNumber* loanAmount = [loan objectForKey:@"loan_amount"]; 
    //float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue]; 

     //NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"]; 

    //set the text to the label 
    //label.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f, please help", 
    //   name,country,outstandingAmount 
    //   ];  
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO; 
    [tbl reloadData]; 
} 


    NSLog(@"url ===%@",urlString); 
    // Create NSURL string from formatted string, by calling the Flickr API 
    NSURL *url = [NSURL URLWithString:urlString]; 


    // Setup and start async download 
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url]; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [connection release]; 
    [request release]; 

這是我的請求的代碼,這個叫我每次按下按鈕:

-(IBAction)searchitem:(id)sender 
{[UIApplication sharedApplication].networkActivityIndicatorVisible=YES; 


[tt resignFirstResponder]; 

NSString *urlString = [NSString stringWithFormat:@"http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=%@",tt.text]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 
[[NSURLConnection alloc] initWithRequest:request delegate:self]; 


} 
+0

你是怎麼解析json的? json的一個例子也會有所幫助。 – 2011-05-11 11:36:20

+0

iam從鏈接http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=jack解析它第一次正常工作,但是當我第二次做同樣的事情時error.Thanks for your reply – 2011-05-11 11:45:49

+1

你可以使用你開始解析的代碼嗎? – 2011-05-11 11:49:06

回答

0

的問題是,我沒有鏈接的json框架很好,我想新的框架,可以在GitHub上,它工作正常。

感謝所有貢獻自己的想法,並帶領我正確解決問題的方向。

非常感謝你.........

+1

你會介意給出新框架的鏈接,如果可能的話,你是如何解決問題的?事實上,我有同樣的問題.. – hafedh 2011-12-03 19:24:47

+0

供參考:你也可以使用[NSJSONSerialization](http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html)哪是標準NS *庫的一部分。這樣,您可以避免使用外部框架進行這種天真的轉換。 – 2013-06-03 10:02:10