我是 iPhone 应用程序的新手。我想实现 json 解析。我已经尝试过网络上提供的示例。但是在进行 json 解析时无法找到获取 url 索引值的确切方法。有没有人早点完成。作为初学者,任何人都可以指导我。
等待正面回应
在此先感谢 Iphone 开发人员
我是 iPhone 应用程序的新手。我想实现 json 解析。我已经尝试过网络上提供的示例。但是在进行 json 解析时无法找到获取 url 索引值的确切方法。有没有人早点完成。作为初学者,任何人都可以指导我。
等待正面回应
在此先感谢 Iphone 开发人员
为了你的看法
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:Str_URL]];
// NSLog(@"\n\n\n request ===%@\n\n\n",request);
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
responseData = [[NSMutableData data] init];
} else {
// Inform the user that the connection failed.
}
#pragma mark NSURLConnection Delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//NSLog(@"\n\n\n response ===%@\n\n\n",response);
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//NSLog(@"\n\n\n data ===%@\n\n\n",data);
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSString *strerror = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
NSLog(@"\n\n\n error ===%@\n\n\n",strerror);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(responseData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
}
}
尝试这个
-(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
{
NSLog(@"ERROR with theConenction");
[connection release];
responseData = nil;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
responseData = nil;
NSLog(@"response String= %@",responseString);
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:responseString error:nil];
NSDictionary *menu = (NSDictionary *) [data objectForKey:@"MainTag"];
NSString *com=(NSString *) [menu objectForKey:@"yourSubTag"];
}