我有一個json字符串和另一個。我必須一起追加,發送到服務器並獲取用戶ID。我困在中間。附加的字符串不會轉換爲NSURL。iOS中的JSON轉換問題
這裏是我的代碼。
- (void)convertingstringtojsondata
{
NSArray *components = [[NSArray alloc]initWithObjects:ismunicipality,mobilenumberstring,placestring,namestring, nil];
NSArray *keys = [[NSArray alloc]initWithObjects:@"is_municipality",@"ph_nbr",@"place",@"reg_name", nil];
NSLog(@"%@",components);
NSDictionary *dict = [[NSDictionary alloc]initWithObjects:components forKeys:keys];
NSLog(@"dict %@", dict);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:nil];
NSLog(@"JSON DATA %@", jsonData);
jsonData= jsonData;
JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(@"JSON STRING%@",JSONString);
}
-(void)sendjsonforregistration
{
[self convertingstringtojsondata];
registerwithserverstring = [[NSMutableString alloc]initWithString:@"http://forthepeople.mcms.net.in/forthepeople_service/registration?reg="];
[registerwithserverstring appendString:JSONString];
NSLog(@" appended string %@", registerwithserverstring);
NSURL *registerURL = [NSURL URLWithString:registerwithserverstring];
NSLog(@"register URL%@", registerURL);
NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:registerURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0];
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * response, NSData * data, NSError * error) {
NSData * jsonData1 = [NSData dataWithContentsOfURL:registerURL];
NSLog(@"%@", jsonData1);
dataDictionary1 = [NSJSONSerialization JSONObjectWithData:jsonData1 options:0 error:&error];
NSLog(@"DATA DICTIONARY %@", dataDictionary1);
}];
}
問題: - 的REGISTERURL顯示空值...
一個長時間的調試我得到了最後的字符串,具體如下:http://forthepeople.mcms.net.in/forthepeople_service/registration?reg={"reg_name 「:」 ugh「,」place「:」Malappuram「,」ph_nbr「:」7204412649「,」is_municipality「:」1「}。但是這個沒有被轉換成NSURL。 –
hey Alvin,你能告訴我你是否想用GET或POST類型發送數據。爲什麼要在JSON中編碼Get查詢字符串? –
您可以發佈此請求的正面回覆,同時註冊成功嗎? –