解析Json时出错

发布于 2024-11-06 14:47:43 字数 3783 浏览 0 评论 0原文

解析 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];


}

When parsing json service , it works fine for first time , but during second time it gave me the error given below

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

Thanks in advance

Code start parsing:

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

parsing code ::

    - (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];

This is the code of my request, this called every time i press button :

-(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];


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2024-11-13 14:47:43

问题是,我没有很好地链接我的 json 框架,我尝试了 github 上提供的新框架,它工作正常。

感谢所有贡献想法并引导我找到解决问题的正确方向的人。

非常感谢.........

The problem was , i did not link my json framework well , i tried new framework available at github and it works fine.

Thanks for all who contributed their ideas and lead me to right direction for solving the problem .

Thank you very much.........

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文