简体   繁体   English

为什么数组和字典的值在json解析中显示为null?

[英]Why value of array and dictionary is showing null in json parsing?

hi i am new to iOS development. 嗨,我是iOS开发的新手。 In above code i am calling a web service, which response is in json format. 在上面的代码中,我正在调用Web服务,其响应为json格式。 am want to do json parsing with response of that web service. 我想用该Web服务的响应进行json解析。

here, why value of json and registartion is showing null in console plz help me. 在这里,为什么json和registartion的值在控制台plz中显示为null可以帮助我。

here is my WebSerVice.h file 这是我的WebSerVice.h文件

@interface WebService : NSObject<NSURLConnectionDelegate ,NSURLConnectionDataDelegate> {
NSMutableData *data;
NSArray  * registrationDetail;
}
-(void)callWebService;
@end

here my web WebService.m file 这是我的Web WebService.m文件

@implementation WebService

-(void)callWebService {
NSString *JsonString =[ NSString stringWithFormat:@"http://bia.igreentechservices.com/ws/ws_registration.php?emailid=%@&password=%@&fname=%@&lname=%@&gender=%@&birthdate=%@&mobile=%@&address=%@&emergency_contact_no=%@&other_contact_no_1=%@&other_contact_no_2=%@",emailAdd,pwd,fName,lName,gen,bDate,mobileNum,add,emergencyContactNum,anotherContactNum1,anothercontactNum2]


   UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
   NSURL *url = [NSURL URLWithString:JsonString];
   NSURLRequest *request =[NSURLRequest  requestWithURL:url];
   NSURLConnection *myConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
   [myConnection start];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {


 data= [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
[data appendData:theData];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *responseString = [[NSString alloc]
    initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
registrationDetail = [json objectForKey:@"registration"];
NSLog(@"%@",json);
NSLog(@"%@",registrationDetail);
NSLog(@"%@",responseString);
NSLog(@"%@",data);

} }

may be in your URL string inculding some whiteSpace you must add %20 in white-space. 可能在您的URL字符串中包含一些空白,您必须在空白中添加%20。 you just add bellow line of code :- 您只需添加以下代码行:-

JsonString = [JsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

you also called you method using NSURLConnection like bellow:- 您还使用NSURLConnection调用了您的方法,例如:

-(void)callWebService {
NSString *JsonString =[ NSString stringWithFormat:@"http://bia.igreentechservices.com/ws/ws_registration.php?emailid=%@&password=%@&fname=%@&lname=%@&gender=%@&birthdate=%@&mobile=%@&address=%@&emergency_contact_no=%@&other_contact_no_1=%@&other_contact_no_2=%@",emailAdd,pwd,fName,lName,gen,bDate,mobileNum,add,emergencyContactNum,anotherContactNum1,anothercontactNum2]


   UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
   NSURL *url = [NSURL URLWithString:JsonString];
   NSURLRequest *request =[NSURLRequest  requestWithURL:url];
   NSURLConnection *myConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(myConnection)
        {
            data=[[NSMutableData alloc]init]; 
        }
        else
        {
            NSLog(@"connection not complet")
        }

}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [data setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [data appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"Error is %@",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *responseString = [[NSString alloc] initWithData:self.webData encoding:NSUTF8StringEncoding];

    data = nil;
    [connection release];
    [data release];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM